spring - 如何修复 springfox 中的 "Security scope definition global could not be resolved"?

标签 spring spring-boot swagger springfox

我正在使用 springfox 从 Spring Controller 生成 swagger 文档。访问时http://127.0.0.1:8080/mycontextroot/swagger-ui.html我有 Swagger 的 UI 作品!

但是当我尝试打开相同的 yaml (或 json)从 http://127.0.0.1:8080/mycontextroot/v2/api-docs 生成的文件但通过 https://editor.swagger.io/我有错误:

enter image description here

Swagger 示例:

---
swagger: '2.0'
info:
    description: Api Documentation
    version: '1.0'
    title: Api Documentation
    termsOfService: urn:tos
    contact: {}
    license:
        name: Apache 2.0
        url: http://www.apache.org/licenses/LICENSE-2.0
host: 127.0.0.1:8080
basePath: "/"
paths:
    "/mycontextroot/blogs":
        get:
            summary: blogs
            operationId: blogsUsingGET
            produces:
                - "*/*"
            responses:
                '200':
                    description: OK
                    schema:
                        "$ref": "#/definitions/Blogs"
                '401':
                    description: Unauthorized
                '403':
                    description: Forbidden
                '404':
                    description: Not Found
            security:
                - xauth:
                      - global
            deprecated: false
securityDefinitions:
    xauth:
        type: apiKey
        name: my-auth-header
        in: header
definitions:
    Blog:
        type: object
        properties:
            title:
                type: string
        title: Blog
    Blogs:
        type: object
        properties:
            blogs:
                type: array
                items:
                    "$ref": "#/definitions/Blog"
        title: Blogs

最佳答案

我遇到过同样的问题。无效的原因是:

security:
    - xauth:
        - global

那需要是:
security:
    - xauth: []

如果您通过 Java 生成 swagger,请应用:
private List<SecurityReference> defaultAuth() {
    return Lists.newArrayList(new SecurityReference("xauth", new AuthorizationScope[0]));
}

关于spring - 如何修复 springfox 中的 "Security scope definition global could not be resolved"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54648227/

相关文章:

java - Spring sseEmitter,调用方法发送后不会立即发送事件

java - Spring Boot中的自定义错误页面

java - 使用 Servlet 配置 Spring Ioc

java - @PersistenceContext 不适用于参数。如何通过构造函数注入(inject)EntityManager?

angular - Spring云服务- Eureka

java - 无法扫描jar文件:错误的魔数(Magic Number)

java - Mybatis 一对多的集合映射总是有一个默认实体

java - 在唯一约束 JPA/Hibernate 期间添加自定义错误消息的更好方法

angular - Swagger 代码生成 --> angular6 : rxjs observable compile error

spring - 来自 Spring Hateoas 的文档 HAL "_links"( Swagger )?