2023-02-16 15:28:15 +08:00
|
|
|
|
package com.zhgd.config;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Function;
|
|
|
|
|
|
import com.google.common.base.Optional;
|
|
|
|
|
|
import com.google.common.base.Predicate;
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
import springfox.documentation.RequestHandler;
|
|
|
|
|
|
import springfox.documentation.builders.ApiInfoBuilder;
|
|
|
|
|
|
import springfox.documentation.builders.PathSelectors;
|
|
|
|
|
|
import springfox.documentation.service.ApiInfo;
|
2023-08-10 10:50:29 +08:00
|
|
|
|
import springfox.documentation.service.ApiKey;
|
|
|
|
|
|
import springfox.documentation.service.AuthorizationScope;
|
|
|
|
|
|
import springfox.documentation.service.SecurityReference;
|
2023-02-16 15:28:15 +08:00
|
|
|
|
import springfox.documentation.spi.DocumentationType;
|
2023-08-10 10:50:29 +08:00
|
|
|
|
import springfox.documentation.spi.service.contexts.SecurityContext;
|
2023-02-16 15:28:15 +08:00
|
|
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
|
|
|
|
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
|
|
|
|
|
2023-08-10 10:50:29 +08:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
2023-02-16 15:28:15 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @program: Iot
|
|
|
|
|
|
* @description: Swagger配置类
|
|
|
|
|
|
* @author: Mr.Wang
|
|
|
|
|
|
* @create: 2019-04-16 14:46
|
|
|
|
|
|
**/
|
|
|
|
|
|
@Configuration
|
|
|
|
|
|
@EnableSwagger2
|
|
|
|
|
|
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
|
|
|
|
|
|
public class SwaggerConfig {
|
|
|
|
|
|
// 定义分隔符
|
|
|
|
|
|
private static final String splitor = ";";
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
2023-08-08 18:20:42 +08:00
|
|
|
|
public Docket createZwRestApi() {
|
2023-08-15 15:17:54 +08:00
|
|
|
|
return new Docket(DocumentationType.SWAGGER_2).groupName("2.政务接口").apiInfo(apiInfo("政务管理API", "政务前后端联调api文档", "1.0")).select()
|
2023-02-16 15:28:15 +08:00
|
|
|
|
//.apis(RequestHandlerSelectors.basePackage("com.zhgd.xmgl"))
|
2023-08-10 10:50:29 +08:00
|
|
|
|
.apis(basePackage("com.zhwl.zw")).paths(PathSelectors.any())
|
2023-08-08 18:20:42 +08:00
|
|
|
|
//.paths(PathSelectors.regex("/zw.*"))
|
2023-02-16 15:28:15 +08:00
|
|
|
|
.build();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
2023-08-08 18:20:42 +08:00
|
|
|
|
public Docket createRestApi() {
|
2023-08-15 15:17:54 +08:00
|
|
|
|
return new Docket(DocumentationType.SWAGGER_2).groupName("1.项目接口").apiInfo(apiInfo("项目管理API", "项目前后端联调api文档", "1.0")).select()
|
2023-02-16 15:28:15 +08:00
|
|
|
|
//.apis(RequestHandlerSelectors.basePackage("com.zhgd.xmgl"))
|
2023-08-08 18:20:42 +08:00
|
|
|
|
.apis(basePackage("com.zhgd.xmgl"))
|
|
|
|
|
|
//.apis(basePackage("com.zhgd.xmgl"+splitor+"com.zhgd.xmgl.device"))
|
2023-02-16 15:28:15 +08:00
|
|
|
|
.paths(PathSelectors.any())
|
2023-08-08 18:20:42 +08:00
|
|
|
|
|
|
|
|
|
|
//.paths(PathSelectors.regex("/xmgl.*"))
|
2023-08-10 10:50:29 +08:00
|
|
|
|
.build()
|
|
|
|
|
|
/* 设置安全模式,swagger可以设置访问token */.securitySchemes(securitySchemes()).securityContexts(securityContexts());
|
2023-02-16 15:28:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-08 18:20:42 +08:00
|
|
|
|
private ApiInfo apiInfo(String titleName, String description, String version) {
|
2023-08-10 10:50:29 +08:00
|
|
|
|
return new ApiInfoBuilder().title(titleName).description(description).version(version).build();
|
2023-02-16 15:28:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Predicate<RequestHandler> basePackage(final String basePackage) {
|
|
|
|
|
|
return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
|
|
|
|
|
|
}
|
2023-08-08 18:20:42 +08:00
|
|
|
|
|
|
|
|
|
|
private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
|
2023-02-16 15:28:15 +08:00
|
|
|
|
return input -> {
|
|
|
|
|
|
// 循环判断匹配
|
|
|
|
|
|
for (String strPackage : basePackage.split(splitor)) {
|
|
|
|
|
|
boolean isMatch = input.getPackage().getName().startsWith(strPackage);
|
|
|
|
|
|
if (isMatch) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2023-08-08 18:20:42 +08:00
|
|
|
|
|
2023-02-16 15:28:15 +08:00
|
|
|
|
private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
|
|
|
|
|
|
return Optional.fromNullable(input.declaringClass());
|
|
|
|
|
|
}
|
2023-08-10 10:50:29 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 安全模式,这里指定token通过Authorization头请求头传递
|
|
|
|
|
|
*/
|
|
|
|
|
|
private List<ApiKey> securitySchemes() {
|
|
|
|
|
|
List<ApiKey> apiKeyList = new ArrayList<ApiKey>();
|
|
|
|
|
|
apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
|
|
|
|
|
|
return apiKeyList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 安全上下文
|
|
|
|
|
|
*/
|
|
|
|
|
|
private List<SecurityContext> securityContexts() {
|
|
|
|
|
|
List<SecurityContext> securityContexts = new ArrayList<>();
|
|
|
|
|
|
securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("^(?!auth).*$")).build());
|
|
|
|
|
|
return securityContexts;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 默认的安全上引用
|
|
|
|
|
|
*/
|
|
|
|
|
|
private List<SecurityReference> defaultAuth() {
|
|
|
|
|
|
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
|
|
|
|
|
|
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
|
|
|
|
|
authorizationScopes[0] = authorizationScope;
|
|
|
|
|
|
List<SecurityReference> securityReferences = new ArrayList<>();
|
|
|
|
|
|
securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
|
|
|
|
|
|
return securityReferences;
|
|
|
|
|
|
}
|
2023-02-16 15:28:15 +08:00
|
|
|
|
}
|