首页 > 解决方案 > Springfox 中的授权范围

问题描述

我尝试为我的 Spring 引导应用程序配置 swagger。我使用此链接进行配置https://www.baeldung.com/spring-boot-swagger-jwt。我改变了一些部分

@Bean
public Docket swaggerCustomConfiguration() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiDetails())
            .securityContexts(Collections.singletonList(securityContext()))
            .securitySchemes(Collections.singletonList(apiKey()))
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .paths(Predicates.not(PathSelectors.regex("/error.*")))
            .build()
            .protocols(swaggerProtocols)
            .host(swaggerHostUrl);
}

private ApiInfo apiDetails() {
    return new ApiInfoBuilder()
            .title(swaggerTitle)
            .description(swaggerDescription)
            .termsOfServiceUrl(termsOfServiceUrl)
            .version("v" + serverVersionCode + "-" + serverVersionName)
            .build();
}

private ApiKey apiKey() {
    return new ApiKey("Bearer", "Authorization", "header");
}

private SecurityContext securityContext() {
    return SecurityContext.builder().securityReferences(defaultAuth()).build();
}

private List<SecurityReference> defaultAuth() {
    final AuthorizationScope readScope = new AuthorizationScope("read", "read scope");
    final AuthorizationScope writeScope = new AuthorizationScope("write", "write scope");
    return Collections.singletonList(new SecurityReference("Bearer",
            new AuthorizationScope[]{readScope, writeScope}));
}

但我不知道的是AuthorizationScope。我不知道到底在做AuthorizationScope什么。可以介绍任何链接来帮助我。

标签: spring-bootswaggerspringfox

解决方案


我知道这不完全是您问题的答案,但您可以考虑转向 springdoc。它是更新的,在我看来,使用库更好、更容易。看看它:

Springfox 已经成为使用如此多的错误和难以配置的功能的痛苦。


推荐阅读