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

问题描述 投票:0回答:1

我一直在尝试在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()));
  }

谁能请一些指点?谢谢。

java spring-boot swagger swagger-ui
1个回答
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");
}
}
© www.soinside.com 2019 - 2024. All rights reserved.