【问题标题】:How fix "Security scope definition global could not be resolved" in springfox?如何在springfox中修复“无法解析全局安全范围定义”?
【发布时间】:2025-12-30 11:05:10
【问题描述】:

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

但是当我尝试打开从http://127.0.0.1:8080/mycontextroot/v2/api-docs 生成的相同yaml(或json)文件但通过https://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

【问题讨论】:

  • 有人解决这个问题吗?

标签: spring spring-boot swagger springfox


【解决方案1】:

我有同样的问题。无效的原因是:

security:
    - xauth:
        - global

必须是:

security:
    - xauth: []

如果你用Java生成swagger,应用:

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

【讨论】:

  • 这修复了规范验证,但破坏了 swagger-ui 中“试用”的身份验证。
【解决方案2】:

@igor-lopatka 的回答是正确的,但让我们尝试详细说明一下:

  1. 没有空的AuthorizationScope 列表只能在OAuth 的情况下使用 身份验证为 scopes 是来自 oAuth World 的实体
  2. 对于其他身份验证架构,它应该为空

查看示例我的应用程序支持两种模式 BasicAuthOAuth

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                // skip irrelevant configuration
                .securitySchemes(Arrays.asList(basicAuth(), oAuth2()))
                .securityContexts(singletonList(securityContext()))
                // skip irrelevant configuration
                .build()
    }

    private BasicAuth basicAuth() {
        return new BasicAuth("basicAuth");
    }

    private OAuth oAuth2() {
        return new OAuth("oAuth2", Arrays.asList(oAuth2AuthorizationScopes()), singletonList(new ResourceOwnerPasswordCredentialsGrant("https://example.com/oauth/token"))));
    }

    private AuthorizationScope[] oAuth2AuthorizationScopes() {
        return new AuthorizationScope[]{
               new AuthorizationScope("read", "read access"),
               new AuthorizationScope("write", "write access")
        };
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(regex("/api/.*"))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        return Arrays.asList(
                new SecurityReference("basicAuth", new AuthorizationScope[]{}),
                new SecurityReference("oAuth2", oAuth2AuthorizationScopes())
        );
    }

【讨论】: