如何修复springfox中的“无法解析全局安全范围定义”?

Che*_*rry 5 spring swagger spring-boot springfox

我正在使用springfox从spring控制器生成swagger文档。访问时,http://127.0.0.1:8080/mycontextroot/swagger-ui.html我得到了有效的用户界面

但是,当我尝试打开通过生成的相同yaml(或json)文件时,出现了错误:http://127.0.0.1:8080/mycontextroot/v2/api-docshttps://editor.swagger.io/

在此处输入图片说明

昂首阔步:

---
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
Run Code Online (Sandbox Code Playgroud)

小智 5

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

security:
    - xauth:
        - global
Run Code Online (Sandbox Code Playgroud)

那需要是:

security:
    - xauth: []
Run Code Online (Sandbox Code Playgroud)

如果您通过 Java 生成 swagger,请应用:

private List<SecurityReference> defaultAuth() {
    return Lists.newArrayList(new SecurityReference("xauth", new AuthorizationScope[0]));
}
Run Code Online (Sandbox Code Playgroud)