|
@ -1,7 +1,6 @@ |
|
|
package com.dc.web.config; |
|
|
package com.dc.web.config; |
|
|
|
|
|
|
|
|
import com.dc.common.config.WinConfig; |
|
|
import com.dc.common.config.WinConfig; |
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
|
|
import io.swagger.models.auth.In; |
|
|
import io.swagger.models.auth.In; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
@ -20,55 +19,55 @@ import java.util.List; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Swagger2的接口配置 |
|
|
* Swagger2的接口配置 |
|
|
* |
|
|
* |
|
|
* @author win |
|
|
* @author win |
|
|
*/ |
|
|
*/ |
|
|
@Configuration |
|
|
@Configuration |
|
|
public class SwaggerConfig |
|
|
public class SwaggerConfig { |
|
|
{ |
|
|
/** |
|
|
/** 系统基础配置 */ |
|
|
* 系统基础配置 |
|
|
|
|
|
*/ |
|
|
@Autowired |
|
|
@Autowired |
|
|
private WinConfig winConfig; |
|
|
private WinConfig winConfig; |
|
|
|
|
|
|
|
|
/** 是否开启swagger */ |
|
|
/** |
|
|
|
|
|
* 是否开启swagger |
|
|
|
|
|
*/ |
|
|
@Value("${swagger.enabled}") |
|
|
@Value("${swagger.enabled}") |
|
|
private boolean enabled; |
|
|
private boolean enabled; |
|
|
|
|
|
|
|
|
/** 设置请求的统一前缀 */ |
|
|
/** |
|
|
|
|
|
* 设置请求的统一前缀 |
|
|
|
|
|
*/ |
|
|
@Value("${swagger.pathMapping}") |
|
|
@Value("${swagger.pathMapping}") |
|
|
private String pathMapping; |
|
|
private String pathMapping; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 创建API |
|
|
* 创建API |
|
|
*/ |
|
|
*/ |
|
|
@Bean |
|
|
@Bean(value = "dockerBean") |
|
|
public Docket createRestApi() |
|
|
public Docket dockerBean() { |
|
|
{ |
|
|
//指定使用Swagger2规范
|
|
|
return new Docket(DocumentationType.OAS_30) |
|
|
Docket docket = new Docket(DocumentationType.SWAGGER_2) |
|
|
// 是否启用Swagger
|
|
|
|
|
|
.enable(enabled) |
|
|
|
|
|
// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
|
|
|
|
|
|
.apiInfo(apiInfo()) |
|
|
.apiInfo(apiInfo()) |
|
|
// 设置哪些接口暴露给Swagger展示
|
|
|
//分组名称
|
|
|
|
|
|
.groupName("用户服务") |
|
|
.select() |
|
|
.select() |
|
|
// 扫描所有有注解的api,用这种方式更灵活
|
|
|
//这里指定Controller扫描包路径
|
|
|
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
|
|
.apis(RequestHandlerSelectors.basePackage("com.dc.app.controller")) |
|
|
// 扫描指定包中的swagger注解
|
|
|
|
|
|
// .apis(RequestHandlerSelectors.basePackage("com.dc.project.tool.swagger"))
|
|
|
|
|
|
// 扫描所有 .apis(RequestHandlerSelectors.any())
|
|
|
|
|
|
.paths(PathSelectors.any()) |
|
|
.paths(PathSelectors.any()) |
|
|
.build() |
|
|
.build() |
|
|
/* 设置安全模式,swagger可以设置访问token */ |
|
|
/* 设置安全模式,swagger可以设置访问token */ |
|
|
.securitySchemes(securitySchemes()) |
|
|
.securitySchemes(securitySchemes()) |
|
|
.securityContexts(securityContexts()) |
|
|
.securityContexts(securityContexts()) |
|
|
.pathMapping(pathMapping); |
|
|
.pathMapping(pathMapping); |
|
|
|
|
|
return docket; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 安全模式,这里指定token通过Authorization头请求头传递 |
|
|
* 安全模式,这里指定token通过Authorization头请求头传递 |
|
|
*/ |
|
|
*/ |
|
|
private List<SecurityScheme> securitySchemes() |
|
|
private List<SecurityScheme> securitySchemes() { |
|
|
{ |
|
|
|
|
|
List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>(); |
|
|
List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>(); |
|
|
apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue())); |
|
|
apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue())); |
|
|
return apiKeyList; |
|
|
return apiKeyList; |
|
@ -77,8 +76,7 @@ public class SwaggerConfig |
|
|
/** |
|
|
/** |
|
|
* 安全上下文 |
|
|
* 安全上下文 |
|
|
*/ |
|
|
*/ |
|
|
private List<SecurityContext> securityContexts() |
|
|
private List<SecurityContext> securityContexts() { |
|
|
{ |
|
|
|
|
|
List<SecurityContext> securityContexts = new ArrayList<>(); |
|
|
List<SecurityContext> securityContexts = new ArrayList<>(); |
|
|
securityContexts.add( |
|
|
securityContexts.add( |
|
|
SecurityContext.builder() |
|
|
SecurityContext.builder() |
|
@ -91,8 +89,7 @@ public class SwaggerConfig |
|
|
/** |
|
|
/** |
|
|
* 默认的安全上引用 |
|
|
* 默认的安全上引用 |
|
|
*/ |
|
|
*/ |
|
|
private List<SecurityReference> defaultAuth() |
|
|
private List<SecurityReference> defaultAuth() { |
|
|
{ |
|
|
|
|
|
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
|
|
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); |
|
|
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
|
|
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; |
|
|
authorizationScopes[0] = authorizationScope; |
|
|
authorizationScopes[0] = authorizationScope; |
|
@ -104,8 +101,7 @@ public class SwaggerConfig |
|
|
/** |
|
|
/** |
|
|
* 添加摘要信息 |
|
|
* 添加摘要信息 |
|
|
*/ |
|
|
*/ |
|
|
private ApiInfo apiInfo() |
|
|
private ApiInfo apiInfo() { |
|
|
{ |
|
|
|
|
|
// 用ApiInfoBuilder进行定制
|
|
|
// 用ApiInfoBuilder进行定制
|
|
|
return new ApiInfoBuilder() |
|
|
return new ApiInfoBuilder() |
|
|
// 设置标题
|
|
|
// 设置标题
|
|
|