拆分think模块包
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 系统配置属性
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2021-08-30 17:58:16
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "config")
|
||||
public class ConfigProperties {
|
||||
/**
|
||||
* 文件上传磁盘位置
|
||||
*/
|
||||
private Integer uploadLocation = 0;
|
||||
|
||||
/**
|
||||
* 文件上传是否使用uuid命名
|
||||
*/
|
||||
private Boolean uploadUuidName = true;
|
||||
|
||||
/**
|
||||
* 文件上传生成缩略图的大小(kb)
|
||||
*/
|
||||
private Integer thumbnailSize = 60;
|
||||
|
||||
/**
|
||||
* OpenOffice的安装目录
|
||||
*/
|
||||
private String openOfficeHome;
|
||||
|
||||
/**
|
||||
* swagger扫描包
|
||||
*/
|
||||
private String swaggerBasePackage;
|
||||
|
||||
/**
|
||||
* swagger文档标题
|
||||
*/
|
||||
private String swaggerTitle;
|
||||
|
||||
/**
|
||||
* swagger文档描述
|
||||
*/
|
||||
private String swaggerDescription;
|
||||
|
||||
/**
|
||||
* swagger文档版本号
|
||||
*/
|
||||
private String swaggerVersion;
|
||||
|
||||
/**
|
||||
* swagger地址
|
||||
*/
|
||||
private String swaggerHost;
|
||||
|
||||
/**
|
||||
* token过期时间, 单位秒
|
||||
*/
|
||||
private Long tokenExpireTime = 60 * 60 * 30 * 24L;
|
||||
|
||||
/**
|
||||
* token快要过期自动刷新时间, 单位分钟
|
||||
*/
|
||||
private int tokenRefreshTime = 30;
|
||||
|
||||
/**
|
||||
* 生成token的密钥Key的base64字符
|
||||
*/
|
||||
private String tokenKey;
|
||||
|
||||
/**
|
||||
* 文件上传目录
|
||||
*/
|
||||
private String uploadPath;
|
||||
|
||||
/**
|
||||
* 本地文件上传目录(开发环境)
|
||||
*/
|
||||
private String localUploadPath;
|
||||
|
||||
/**
|
||||
* 文件服务器
|
||||
*/
|
||||
private String fileServer;
|
||||
|
||||
/**
|
||||
* 网关地址
|
||||
*/
|
||||
private String serverUrl;
|
||||
|
||||
/**
|
||||
* 阿里云存储 OSS
|
||||
* Endpoint
|
||||
*/
|
||||
private String endpoint;
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String bucketName;
|
||||
private String bucketDomain;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class HttpMessageConverter extends MappingJackson2HttpMessageConverter {
|
||||
public HttpMessageConverter(){
|
||||
List<MediaType> mediaTypes = new ArrayList<>();
|
||||
mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
setSupportedMediaTypes(mediaTypes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import net.sf.jsqlparser.expression.NullValue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* MybatisPlus配置
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2018-02-22 11:29:28
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor(HttpServletRequest request) {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
|
||||
// 多租户插件配置
|
||||
TenantLineHandler tenantLineHandler = new TenantLineHandler() {
|
||||
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
// 从域名拿ID
|
||||
String Domain = request.getHeader("Domain");
|
||||
if (StrUtil.isNotBlank(Domain)) {
|
||||
String key = "Domain:" + Domain;
|
||||
String tenantId = redisUtil.get(key);
|
||||
if(tenantId != null){
|
||||
System.out.println("授权域名" + Domain + " => " + tenantId);
|
||||
return new LongValue(tenantId);
|
||||
}
|
||||
}
|
||||
// 从请求头拿ID
|
||||
final String tenantId = request.getHeader("tenantId");
|
||||
if(tenantId != null){
|
||||
return new LongValue(tenantId);
|
||||
}
|
||||
return getLoginUserTenantId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoreTable(String tableName) {
|
||||
return Arrays.asList(
|
||||
"sys_tenant",
|
||||
"sys_dictionary",
|
||||
"sys_dictionary_data",
|
||||
"sys_user_oauth",
|
||||
"sys_email_record",
|
||||
"sys_plug",
|
||||
"sys_version",
|
||||
"sys_order",
|
||||
"sys_white_domain"
|
||||
).contains(tableName);
|
||||
}
|
||||
};
|
||||
TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(tenantLineHandler);
|
||||
interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
|
||||
|
||||
// 分页插件配置
|
||||
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
|
||||
interceptor.addInnerInterceptor(paginationInnerInterceptor);
|
||||
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户的租户id
|
||||
*
|
||||
* @return Integer
|
||||
*/
|
||||
public Expression getLoginUserTenantId() {
|
||||
try {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null) {
|
||||
Object object = authentication.getPrincipal();
|
||||
if (object instanceof User) {
|
||||
return new LongValue(((User) object).getTenantId());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return new NullValue();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
|
||||
RestTemplate restTemplate = new RestTemplate(factory);
|
||||
restTemplate.getMessageConverters().add(new HttpMessageConverter());
|
||||
return restTemplate;
|
||||
}
|
||||
@Bean
|
||||
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
|
||||
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||
// ms
|
||||
factory.setReadTimeout(60000);
|
||||
// ms
|
||||
factory.setConnectTimeout(60000);
|
||||
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author ds
|
||||
* @Date 2022-05-05
|
||||
*/
|
||||
@Component
|
||||
public class SpringContextUtil implements ApplicationContextAware {
|
||||
/**
|
||||
* spring的应用上下文
|
||||
*/
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 初始化时将应用上下文设置进applicationContext
|
||||
* @param applicationContext
|
||||
* @throws BeansException
|
||||
*/
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
SpringContextUtil.applicationContext=applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext(){
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据bean名称获取某个bean对象
|
||||
*
|
||||
* @param name bean名称
|
||||
* @return Object
|
||||
* @throws BeansException
|
||||
*/
|
||||
public static Object getBean(String name) throws BeansException {
|
||||
return applicationContext.getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据bean的class获取某个bean对象
|
||||
* @param beanClass
|
||||
* @param <T>
|
||||
* @return
|
||||
* @throws BeansException
|
||||
*/
|
||||
public static <T> T getBean(Class<T> beanClass) throws BeansException {
|
||||
return applicationContext.getBean(beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取spring.profiles.active
|
||||
* @return
|
||||
*/
|
||||
public static String getProfile(){
|
||||
return getApplicationContext().getEnvironment().getActiveProfiles()[0];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.oas.annotations.EnableOpenApi;
|
||||
import springfox.documentation.service.*;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Swagger配置
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2018-02-22 11:29:05
|
||||
*/
|
||||
@EnableOpenApi
|
||||
@Configuration
|
||||
public class SwaggerConfig {
|
||||
@Resource
|
||||
private ConfigProperties config;
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
Docket docket = new Docket(DocumentationType.OAS_30);
|
||||
if (StrUtil.isNotBlank(config.getSwaggerHost())) {
|
||||
docket.host(config.getSwaggerHost());
|
||||
}
|
||||
return docket
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage(config.getSwaggerBasePackage()))
|
||||
.paths(PathSelectors.any())
|
||||
.build()
|
||||
.securitySchemes(securitySchemes())
|
||||
.securityContexts(securityContexts());
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title(config.getSwaggerTitle())
|
||||
.description(config.getSwaggerDescription())
|
||||
.version(config.getSwaggerVersion())
|
||||
.termsOfServiceUrl("")
|
||||
.build();
|
||||
}
|
||||
|
||||
private List<SecurityScheme> securitySchemes() {
|
||||
return Collections.singletonList(
|
||||
new ApiKey("Authorization", "Authorization", "header")
|
||||
);
|
||||
}
|
||||
|
||||
private List<SecurityContext> securityContexts() {
|
||||
AuthorizationScope[] scopes = {new AuthorizationScope("global", "accessEverything")};
|
||||
List<SecurityReference> references = Collections.singletonList(
|
||||
new SecurityReference("Authorization", scopes)
|
||||
);
|
||||
return Collections.singletonList(SecurityContext.builder()
|
||||
.securityReferences(references)
|
||||
.build());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* WebMvc配置, 拦截器、资源映射等都在此配置
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2019-06-12 10:11:16
|
||||
*/
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
/**
|
||||
* 支持跨域访问
|
||||
*/
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOriginPatterns("*")
|
||||
.allowedHeaders("*")
|
||||
.exposedHeaders(Constants.TOKEN_HEADER_NAME)
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
|
||||
.allowCredentials(true)
|
||||
.maxAge(3600);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user