qinxi
2021-04-26 11:42:47 +08:00
如果是 auth 类 header 会有个小锁
new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xx"))
.paths(PathSelectors.any())
.build().securitySchemes(securitySchemes())
//.globalOperationParameters(globalOperationParameters())
.securityContexts(securityContexts());
private List<ApiKey> securitySchemes() {
ArrayList<ApiKey> list = new ArrayList<>();
list.add(new ApiKey("Authorization", "Authorization", "header"));
return list;
}
private List<SecurityContext> securityContexts() {
return Collections.singletonList(
SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.any())
.build()
);
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(
new SecurityReference("Authorization", authorizationScopes));
}
//如果是普通的 header
new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xx"))
.paths(PathSelectors.any())
.build()
//.securitySchemes(securitySchemes())
.globalOperationParameters(globalOperationParameters())
;
private List<Parameter> globalOperationParameters() {
ArrayList<Parameter> list = new ArrayList<>();
Parameter parameter = new ParameterBuilder().name("key").required(true)
.description("").parameterType("header").defaultValue("")
.allowMultiple(false)
.modelRef(new ModelRef("string")).build();
list.add(parameter);
return list;
}