Swagger2 配置

1. 每个请求都需要换取key:

@Bean
public Docket createRestApi() {
//添加head参数start
ParameterBuilder appId = new ParameterBuilder();
ParameterBuilder tokenCode = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
appId.name("AppId").description("客户端编号").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
tokenCode.name("AppToken").description("Token令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(appId.build());
pars.add(tokenCode.build());
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.inuo.project.tool.swagger"))
.paths(PathSelectors.any()).build()
.globalOperationParameters(pars)
// 详细定制
.apiInfo(apiInfo());
} /**
* 添加摘要信息
*/
private ApiInfo apiInfo() {
// 用ApiInfoBuilder进行定制
return new ApiInfoBuilder().title("标题:XX管理系统_接口文档").description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
.contact(new Contact(inuoConfig.getName(), null, null)).version("版本号:" + inuoConfig.getVersion())
.build();
}

  

2. 全局Key

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).
useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("^(?!auth).*$"))
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts())
;
}
private List<ApiKey> securitySchemes() {
List<ApiKey> list = new ArrayList<ApiKey>();
ApiKey ak = new ApiKey("Authorization", "Authorization", "header");
list.add(ak);
return list;
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> list = new ArrayList<SecurityContext>();
SecurityContext sc = SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("^(?!auth).*$")).build();
list.add(sc);
return list;
} List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List<SecurityReference> list = new ArrayList<SecurityReference>();
SecurityReference sr = new SecurityReference("Authorization", authorizationScopes);
list.add(sr);
return list;
}

  

上一篇:Jmeter之JDBC请求(四)


下一篇: