【问题标题】:How to configure jwt token in swagger header using spring boot如何使用 Spring Boot 在 swagger 标头中配置 jwt 令牌
【发布时间】:2020-07-11 19:24:24
【问题描述】:

我在 Spring Boot 应用程序中配置了 swagger,但我不知道如何验证 jwt 授权令牌,swagger 版本是 2.4.0

@Bean
public Docket newsApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .securitySchemes(Lists.newArrayList(apiKey()))
            .securityContexts(Lists.newArrayList(securityContext()));

}

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

List<SecurityReference> defaultAuth() {
    AuthorizationScope authorizationScope
            = new AuthorizationScope("global", "accessEverything");
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = authorizationScope;
    return Lists.newArrayList(
            new SecurityReference("JWT", authorizationScopes));
}

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

【问题讨论】:

    标签: spring-boot jwt swagger


    【解决方案1】:

    我今天在这个问题上苦苦挣扎,发现实际上没有办法

    您的代码已经足够完成这项任务,只是缺少一件事:当您在 UI 中添加令牌时,您还需要在令牌前添加“Bearer”。

    1. 点击添加您的授权:
    2. 添加“Bearer”+您的令牌:

    我也尝试将“Bearer”添加到ApiKey,但它也不起作用(因为它会生成双“:”。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 2021-09-27
      • 2021-06-20
      • 1970-01-01
      • 2022-01-14
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      相关资源
      最近更新 更多