首页 > 解决方案 > 在使用 Swagger UI 发出的请求中添加授权

问题描述

我一直在尝试在从 swagger-ui 尝试的请求中添加授权,但在请求中,授权标头始终为空。

这就是我所做的。

  private ApiKey apiKey() {
    return new ApiKey("apiKey", "Authorization",
        "header"); //`apiKey` is the name of the APIKey, `Authorization` is the key in the request header
  }

  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select().apis(RequestHandlerSelectors.basePackage("com.example.app"))
        .paths(PathSelectors.any()).build().apiInfo(apiInfo()).securitySchemes(Arrays.asList(apiKey()));
  }

任何人都可以给一些指示吗?谢谢。

标签: javaspring-bootswaggerswagger-ui

解决方案


你可以试试这个 SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
            .build().securitySchemes(Lists.newArrayList(apiKey()));

}

private ApiKey apiKey() {
    return new ApiKey("AUTHORIZATION", "api_key", "header");
}
}

推荐阅读