java - 在使用 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()));
}
任何人都可以给一些指示吗?谢谢。
解决方案
你可以试试这个 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");
}
}
推荐阅读
- flutter - Flutter Forms:获取错误字段列表
- asp.net-core - 客户端中的 IdentityServer 4 身份验证事件
- javascript - 将方括号中的数据转换为 JSON
- uwp - 构建 Microsoft Mixed Reality WebRTC 源代码时出错
- r - R:如何在 Dataframe 中从某一列开始插入一行?
- python - 基于django更新函数的多个模型视图
- api - 如何在spring mvc中调用api并从更新方法中选择正确的可选
- c# - Automapper 自定义值解析器重用多种类型
- python - 将 Json 脚本中的嵌套列表转换为 Dataframe
- flutter - 相关的 DropdownButtons 和引发 onChange 事件