|
|
@ -1,188 +1,182 @@
|
|
|
|
package org.jeecg.config;
|
|
|
|
//package org.jeecg.config;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
//import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
|
|
|
import org.jeecg.common.constant.CommonConstant;
|
|
|
|
//import org.jeecg.common.constant.CommonConstant;
|
|
|
|
import org.jeecg.config.mybatis.MybatisPlusSaasConfig;
|
|
|
|
//import org.springframework.beans.BeansException;
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
//import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
|
//import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
//import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
//import org.springframework.context.annotation.Import;
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
//import org.springframework.util.ReflectionUtils;
|
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
|
//import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
//import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
|
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
//import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
|
|
|
|
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
|
|
|
|
//import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
|
|
|
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
|
|
|
//import springfox.documentation.builders.ApiInfoBuilder;
|
|
|
|
import springfox.documentation.builders.ApiInfoBuilder;
|
|
|
|
//import springfox.documentation.builders.ParameterBuilder;
|
|
|
|
import springfox.documentation.builders.ParameterBuilder;
|
|
|
|
//import springfox.documentation.builders.PathSelectors;
|
|
|
|
import springfox.documentation.builders.PathSelectors;
|
|
|
|
//import springfox.documentation.builders.RequestHandlerSelectors;
|
|
|
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
|
|
|
//import springfox.documentation.oas.annotations.EnableOpenApi;
|
|
|
|
import springfox.documentation.schema.ModelRef;
|
|
|
|
//import springfox.documentation.schema.ModelRef;
|
|
|
|
import springfox.documentation.service.*;
|
|
|
|
//import springfox.documentation.service.*;
|
|
|
|
import springfox.documentation.spi.DocumentationType;
|
|
|
|
//import springfox.documentation.spi.DocumentationType;
|
|
|
|
import springfox.documentation.spi.service.contexts.SecurityContext;
|
|
|
|
//import springfox.documentation.spi.service.contexts.SecurityContext;
|
|
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
|
|
//import springfox.documentation.spring.web.plugins.Docket;
|
|
|
|
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
|
|
|
|
//import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
|
|
|
|
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
|
|
|
|
//import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
|
|
|
|
|
|
|
|
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
//
|
|
|
|
import java.util.ArrayList;
|
|
|
|
//import java.lang.reflect.Field;
|
|
|
|
import java.util.Collections;
|
|
|
|
//import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
//import java.util.Collections;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
//import java.util.List;
|
|
|
|
|
|
|
|
//import java.util.stream.Collectors;
|
|
|
|
/**
|
|
|
|
//
|
|
|
|
* @Author scott
|
|
|
|
///**
|
|
|
|
*/
|
|
|
|
// * @Author scott
|
|
|
|
@Configuration
|
|
|
|
// */
|
|
|
|
@EnableSwagger2WebMvc
|
|
|
|
//@Configuration
|
|
|
|
@Import(BeanValidatorPluginsConfiguration.class)
|
|
|
|
//@EnableSwagger2 //开启 Swagger2
|
|
|
|
public class Swagger2Config implements WebMvcConfigurer {
|
|
|
|
//@EnableKnife4j //开启 knife4j,可以不写
|
|
|
|
|
|
|
|
//@Import(BeanValidatorPluginsConfiguration.class)
|
|
|
|
/**
|
|
|
|
//public class Swagger2Config implements WebMvcConfigurer {
|
|
|
|
*
|
|
|
|
//
|
|
|
|
* 显示swagger-ui.html文档展示页,还必须注入swagger资源:
|
|
|
|
// /**
|
|
|
|
*
|
|
|
|
// *
|
|
|
|
* @param registry
|
|
|
|
// * 显示swagger-ui.html文档展示页,还必须注入swagger资源:
|
|
|
|
*/
|
|
|
|
// *
|
|
|
|
@Override
|
|
|
|
// * @param registry
|
|
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
// */
|
|
|
|
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
|
|
|
|
// @Override
|
|
|
|
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
|
|
|
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
|
|
|
// registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
|
|
|
|
}
|
|
|
|
// registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
|
|
|
|
|
|
|
// registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
|
|
|
/**
|
|
|
|
// }
|
|
|
|
* swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
|
|
|
|
//
|
|
|
|
*
|
|
|
|
// /**
|
|
|
|
* @return Docket
|
|
|
|
// * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
|
|
|
|
*/
|
|
|
|
// *
|
|
|
|
@Bean(value = "defaultApi2")
|
|
|
|
// * @return Docket
|
|
|
|
public Docket defaultApi2() {
|
|
|
|
// */
|
|
|
|
return new Docket(DocumentationType.SWAGGER_2)
|
|
|
|
// @Bean(value = "defaultApi2")
|
|
|
|
.apiInfo(apiInfo())
|
|
|
|
// public Docket defaultApi2() {
|
|
|
|
.select()
|
|
|
|
// return new Docket(DocumentationType.SWAGGER_2)
|
|
|
|
//此包路径下的类,才生成接口文档
|
|
|
|
// .apiInfo(apiInfo())
|
|
|
|
.apis(RequestHandlerSelectors.basePackage("org.jeecg"))
|
|
|
|
// .select()
|
|
|
|
//加了ApiOperation注解的类,才生成接口文档
|
|
|
|
// //此包路径下的类,才生成接口文档
|
|
|
|
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
|
|
|
|
// .apis(RequestHandlerSelectors.basePackage("org.jeecg"))
|
|
|
|
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
|
|
|
// //加了ApiOperation注解的类,才生成接口文档
|
|
|
|
.paths(PathSelectors.any())
|
|
|
|
// .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
|
|
|
|
.build()
|
|
|
|
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
|
|
|
.securitySchemes(Collections.singletonList(securityScheme()))
|
|
|
|
// .paths(PathSelectors.any())
|
|
|
|
.securityContexts(securityContexts())
|
|
|
|
// .build()
|
|
|
|
.globalOperationParameters(setHeaderToken());
|
|
|
|
// .securitySchemes(Collections.singletonList(securityScheme()))
|
|
|
|
}
|
|
|
|
// .securityContexts(securityContexts())
|
|
|
|
|
|
|
|
// .globalOperationParameters(setHeaderToken());
|
|
|
|
/***
|
|
|
|
// }
|
|
|
|
* oauth2配置
|
|
|
|
//
|
|
|
|
* 需要增加swagger授权回调地址
|
|
|
|
// /***
|
|
|
|
* http://localhost:8888/webjars/springfox-swagger-ui/o2c.html
|
|
|
|
// * oauth2配置
|
|
|
|
* @return
|
|
|
|
// * 需要增加swagger授权回调地址
|
|
|
|
*/
|
|
|
|
// * http://localhost:8888/webjars/springfox-swagger-ui/o2c.html
|
|
|
|
@Bean
|
|
|
|
// * @return
|
|
|
|
SecurityScheme securityScheme() {
|
|
|
|
// */
|
|
|
|
return new ApiKey(CommonConstant.X_ACCESS_TOKEN, CommonConstant.X_ACCESS_TOKEN, "header");
|
|
|
|
// @Bean
|
|
|
|
}
|
|
|
|
// SecurityScheme securityScheme() {
|
|
|
|
/**
|
|
|
|
// return new ApiKey(CommonConstant.X_ACCESS_TOKEN, CommonConstant.X_ACCESS_TOKEN, "header");
|
|
|
|
* JWT token
|
|
|
|
// }
|
|
|
|
* @return
|
|
|
|
// /**
|
|
|
|
*/
|
|
|
|
// * JWT token
|
|
|
|
private List<Parameter> setHeaderToken() {
|
|
|
|
// * @return
|
|
|
|
ParameterBuilder tokenPar = new ParameterBuilder();
|
|
|
|
// */
|
|
|
|
List<Parameter> pars = new ArrayList<>();
|
|
|
|
// private List<Parameter> setHeaderToken() {
|
|
|
|
tokenPar.name(CommonConstant.X_ACCESS_TOKEN).description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
|
|
|
|
// ParameterBuilder tokenPar = new ParameterBuilder();
|
|
|
|
pars.add(tokenPar.build());
|
|
|
|
// List<Parameter> pars = new ArrayList<>();
|
|
|
|
//update-begin-author:liusq---date:2024-08-15--for: 开启多租户时,全局参数增加租户id
|
|
|
|
// tokenPar.name(CommonConstant.X_ACCESS_TOKEN).description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
|
|
|
|
if(MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL){
|
|
|
|
// pars.add(tokenPar.build());
|
|
|
|
ParameterBuilder tenantPar = new ParameterBuilder();
|
|
|
|
// return pars;
|
|
|
|
tenantPar.name(CommonConstant.TENANT_ID).description("租户ID").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
|
|
|
|
// }
|
|
|
|
pars.add(tenantPar.build());
|
|
|
|
//
|
|
|
|
}
|
|
|
|
// /**
|
|
|
|
//update-end-author:liusq---date:2024-08-15--for: 开启多租户时,全局参数增加租户id
|
|
|
|
// * api文档的详细信息函数,注意这里的注解引用的是哪个
|
|
|
|
|
|
|
|
// *
|
|
|
|
return pars;
|
|
|
|
// * @return
|
|
|
|
}
|
|
|
|
// */
|
|
|
|
|
|
|
|
// private ApiInfo apiInfo() {
|
|
|
|
/**
|
|
|
|
// return new ApiInfoBuilder()
|
|
|
|
* api文档的详细信息函数,注意这里的注解引用的是哪个
|
|
|
|
// // //大标题
|
|
|
|
*
|
|
|
|
// .title("JeecgBoot 后台服务API接口文档")
|
|
|
|
* @return
|
|
|
|
// // 版本号
|
|
|
|
*/
|
|
|
|
// .version("1.0")
|
|
|
|
private ApiInfo apiInfo() {
|
|
|
|
//// .termsOfServiceUrl("NO terms of service")
|
|
|
|
return new ApiInfoBuilder()
|
|
|
|
// // 描述
|
|
|
|
// //大标题
|
|
|
|
// .description("后台API接口")
|
|
|
|
.title("JeecgBoot 后台服务API接口文档")
|
|
|
|
// // 作者
|
|
|
|
// 版本号
|
|
|
|
// .contact(new Contact("北京国炬信息技术有限公司","www.jeccg.com","jeecgos@163.com"))
|
|
|
|
.version("1.0")
|
|
|
|
// .license("The Apache License, Version 2.0")
|
|
|
|
// .termsOfServiceUrl("NO terms of service")
|
|
|
|
// .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
|
|
|
|
// 描述
|
|
|
|
// .build();
|
|
|
|
.description("后台API接口")
|
|
|
|
// }
|
|
|
|
// 作者
|
|
|
|
//
|
|
|
|
.contact(new Contact("北京国炬信息技术有限公司","www.jeccg.com","jeecgos@163.com"))
|
|
|
|
// /**
|
|
|
|
.license("The Apache License, Version 2.0")
|
|
|
|
// * 新增 securityContexts 保持登录状态
|
|
|
|
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
|
|
|
|
// */
|
|
|
|
.build();
|
|
|
|
// private List<SecurityContext> securityContexts() {
|
|
|
|
}
|
|
|
|
// return new ArrayList(
|
|
|
|
|
|
|
|
// Collections.singleton(SecurityContext.builder()
|
|
|
|
/**
|
|
|
|
// .securityReferences(defaultAuth())
|
|
|
|
* 新增 securityContexts 保持登录状态
|
|
|
|
// .forPaths(PathSelectors.regex("^(?!auth).*$"))
|
|
|
|
*/
|
|
|
|
// .build())
|
|
|
|
private List<SecurityContext> securityContexts() {
|
|
|
|
// );
|
|
|
|
return new ArrayList(
|
|
|
|
// }
|
|
|
|
Collections.singleton(SecurityContext.builder()
|
|
|
|
//
|
|
|
|
.securityReferences(defaultAuth())
|
|
|
|
// private List<SecurityReference> defaultAuth() {
|
|
|
|
.forPaths(PathSelectors.regex("^(?!auth).*$"))
|
|
|
|
// AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
|
|
|
|
.build())
|
|
|
|
// AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
|
|
|
);
|
|
|
|
// authorizationScopes[0] = authorizationScope;
|
|
|
|
}
|
|
|
|
// return new ArrayList(
|
|
|
|
|
|
|
|
// Collections.singleton(new SecurityReference(CommonConstant.X_ACCESS_TOKEN, authorizationScopes)));
|
|
|
|
private List<SecurityReference> defaultAuth() {
|
|
|
|
// }
|
|
|
|
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
|
|
|
|
//
|
|
|
|
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
|
|
|
// /**
|
|
|
|
authorizationScopes[0] = authorizationScope;
|
|
|
|
// * 解决springboot2.6 和springfox不兼容问题
|
|
|
|
return new ArrayList(
|
|
|
|
// * @return
|
|
|
|
Collections.singleton(new SecurityReference(CommonConstant.X_ACCESS_TOKEN, authorizationScopes)));
|
|
|
|
// */
|
|
|
|
}
|
|
|
|
// @Bean
|
|
|
|
|
|
|
|
// public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
|
|
|
|
/**
|
|
|
|
// return new BeanPostProcessor() {
|
|
|
|
* 解决springboot2.6 和springfox不兼容问题
|
|
|
|
//
|
|
|
|
* @return
|
|
|
|
// @Override
|
|
|
|
*/
|
|
|
|
// public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
|
@Bean
|
|
|
|
// if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
|
|
|
|
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
|
|
|
|
// customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
|
|
|
|
return new BeanPostProcessor() {
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return bean;
|
|
|
|
@Override
|
|
|
|
// }
|
|
|
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
|
//
|
|
|
|
if (bean instanceof WebMvcRequestHandlerProvider) {
|
|
|
|
// private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
|
|
|
|
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
|
|
|
|
// List<T> copy = mappings.stream()
|
|
|
|
}
|
|
|
|
// .filter(mapping -> mapping.getPatternParser() == null)
|
|
|
|
return bean;
|
|
|
|
// .collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
// mappings.clear();
|
|
|
|
|
|
|
|
// mappings.addAll(copy);
|
|
|
|
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
|
|
|
|
// }
|
|
|
|
List<T> copy = mappings.stream()
|
|
|
|
//
|
|
|
|
.filter(mapping -> mapping.getPatternParser() == null)
|
|
|
|
// @SuppressWarnings("unchecked")
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
// private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
|
|
|
|
mappings.clear();
|
|
|
|
// try {
|
|
|
|
mappings.addAll(copy);
|
|
|
|
// Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
|
|
|
|
}
|
|
|
|
// field.setAccessible(true);
|
|
|
|
|
|
|
|
// return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
// } catch (IllegalArgumentException | IllegalAccessException e) {
|
|
|
|
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
|
|
|
|
// throw new IllegalStateException(e);
|
|
|
|
try {
|
|
|
|
// }
|
|
|
|
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
|
|
|
|
// }
|
|
|
|
field.setAccessible(true);
|
|
|
|
// };
|
|
|
|
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
|
|
|
|
// }
|
|
|
|
} catch (IllegalArgumentException | IllegalAccessException e) {
|
|
|
|
//
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
//
|
|
|
|
}
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|