1. 添加依赖
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
2. 基础配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket getDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swaggerDemoApiInfo())
.select()
//可以通过apis()方法设置哪个包中内容被扫描
.apis(RequestHandlerSelectors.basePackage("com.lumashequ.cloudstorage.controller"))
.paths(PathSelectors.any())
.build());
}
private ApiInfo swaggerDemoApiInfo() {
return new ApiInfoBuilder()
.contact(new Contact("撸码社区的swagger", "http://www.xxx.com", "xxxqq.com"))
//文档标题
.title("我的后台管理系统")
//文档描述
.description("管理项目")
//文档版本
.version("1.0.0")
.build();
}
}
3. 配置全局Token后的配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket getDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swaggerDemoApiInfo())
.select()
//可以通过apis()方法设置哪个包中内容被扫描
.apis(RequestHandlerSelectors.basePackage("com.lumashequ.cloudstorage.controller"))
.paths(PathSelectors.any())
.build()
// 安全上下文
.securityContexts(Arrays.asList(securityContexts()))
.securitySchemes(unifiedAuth());
}
private ApiInfo swaggerDemoApiInfo() {
return new ApiInfoBuilder()
.contact(new Contact("撸码社区的swagger", "http://www.xxx.com", "xxxqq.com"))
//文档标题
.title("我的后台管理系统")
//文档描述
.description("管理项目")
//文档版本
.version("1.0.0")
.build();
}
// 添加配置全局token
private static List unifiedAuth() {
List arrayList = new ArrayList<>();
arrayList.add(new ApiKey("token", "token", "header"));
return arrayList;
}
private SecurityContext securityContexts() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.any())
.build();
}
private List defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "全局token");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Arrays.asList(new SecurityReference("token", authorizationScopes));
}
}
4. 效果展示