在使用Swagger UI发出的请求中添加授权

时间:2019-03-28 11:48:50

标签: java spring-boot swagger swagger-ui

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

这就是我所做的。

  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()));
  }

谁能给我一些指导?谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用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");
}
}