带有Bearer令牌的Springboot Swagger UI

时间:2018-10-19 13:56:31

标签: spring-boot swagger swagger-ui

我使用Bearer令牌访问我的api。所以我像这样配置我的摇摇欲坠:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("br.com.lumera.intimacaoapi.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Lists.newArrayList(apiKey()))
                .securityContexts(Arrays.asList(securityContext()));
    }


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

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

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope(
                "global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference("Bearer",
                authorizationScopes));
    }
}

,但是对于每个请求,我的用户都必须输入Bearer <>。我可以配置招摇工具,将“ Bearer”自动放置在用户插入的令牌之前吗?

tks

1 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是将“ Bearer”设置为默认值,如下所示

public ResponseEntity method_name(@ApiParam(defaultValue = "Bearer ") String auth) { }

此代码将在令牌输入字段框中将“载体”显示为默认值。您可以在输入字段框中的令牌后面添加令牌。