增加多租户功能(应用隔离)
This commit is contained in:
-21
@@ -1,21 +0,0 @@
|
||||
package com.central.common.annotation;
|
||||
|
||||
import com.central.common.config.LoginArgResolverConfig;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 在启动类上添加该注解来----开启自动登录用户对象注入
|
||||
* Token转化SysUser
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Import(LoginArgResolverConfig.class)
|
||||
public @interface EnableLoginArgResolver {
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.central.common.config;
|
||||
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.interceptor.TenantInterceptor;
|
||||
import com.central.common.resolver.ClientArgumentResolver;
|
||||
import com.central.common.resolver.TokenArgumentResolver;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 默认SpringMVC拦截器
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
public class DefaultWebMvcConfig extends WebMvcConfigurationSupport {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 配置SpringMVC拦截器,添加租户拦截器
|
||||
*/
|
||||
@Override
|
||||
protected void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new TenantInterceptor()).addPathPatterns("/**");
|
||||
super.addInterceptors(registry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Token参数解析
|
||||
*
|
||||
* @param argumentResolvers 解析类
|
||||
*/
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
//注入用户信息
|
||||
argumentResolvers.add(new TokenArgumentResolver(userService));
|
||||
//注入应用信息
|
||||
argumentResolvers.add(new ClientArgumentResolver());
|
||||
}
|
||||
}
|
||||
+5
@@ -97,4 +97,9 @@ public interface CommonConstant {
|
||||
String DEF_USER_PASSWORD = "123456";
|
||||
|
||||
String LOCK_KEY_PREFIX = "LOCK_KEY:";
|
||||
|
||||
/**
|
||||
* 租户id参数
|
||||
*/
|
||||
String TENANT_ID_PARAM = "tenantId";
|
||||
}
|
||||
|
||||
+2
-1
@@ -27,8 +27,9 @@ public interface SecurityConstants {
|
||||
String ROLE_HEADER = "x-role-header";
|
||||
|
||||
/**
|
||||
* 应用信息头
|
||||
* 租户信息头(应用)
|
||||
*/
|
||||
String TENANT_HEADER = "x-tenant-header";
|
||||
String CLIENT_HEADER = "x-client-header";
|
||||
|
||||
/**
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.central.common.interceptor;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.utils.TenantContextHolder;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 租户拦截器
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
public class TenantInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
//优先获取请求参数中的tenantId值
|
||||
String tenantId = request.getParameter(CommonConstant.TENANT_ID_PARAM);
|
||||
if (StrUtil.isEmpty(tenantId)) {
|
||||
tenantId = request.getHeader(SecurityConstants.TENANT_HEADER);
|
||||
}
|
||||
//保存租户id
|
||||
if(StrUtil.isNotEmpty(tenantId)){
|
||||
TenantContextHolder.setTenant(tenantId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.central.common.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 多租户配置
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.tenant")
|
||||
@RefreshScope
|
||||
public class TenantProperties {
|
||||
/**
|
||||
* 是否开启多租户
|
||||
*/
|
||||
private Boolean enable = false;
|
||||
|
||||
/**
|
||||
* 配置不进行多租户隔离的表名
|
||||
*/
|
||||
private List<String> ignoreTables = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 配置不进行多租户隔离的sql
|
||||
* 需要配置mapper的全路径如:com.central.user.mapper.SysUserMapper.findList
|
||||
*/
|
||||
private List<String> ignoreSqls = new ArrayList<>();
|
||||
}
|
||||
+1
-1
@@ -44,7 +44,7 @@ public class ClientArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
NativeWebRequest nativeWebRequest,
|
||||
WebDataBinderFactory webDataBinderFactory) {
|
||||
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
|
||||
String clientId = request.getHeader(SecurityConstants.CLIENT_HEADER);
|
||||
String clientId = request.getHeader(SecurityConstants.TENANT_HEADER);
|
||||
if (StrUtil.isBlank(clientId)) {
|
||||
log.warn("resolveArgument error clientId is empty");
|
||||
}
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.central.common.utils;
|
||||
|
||||
/**
|
||||
* 租户holder
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
public class TenantContextHolder {
|
||||
private static final ThreadLocal<String> CONTEXT = new ThreadLocal<>();
|
||||
|
||||
public static void setTenant(String tenant) {
|
||||
CONTEXT.set(tenant);
|
||||
}
|
||||
|
||||
public static String getTenant() {
|
||||
return CONTEXT.get();
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
CONTEXT.remove();
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,11 @@
|
||||
<artifactId>zlt-db-spring-boot-starter</artifactId>
|
||||
<description>数据库通用组件</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
|
||||
+26
-1
@@ -1,7 +1,14 @@
|
||||
package com.central.db.config;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
|
||||
|
||||
import com.central.common.properties.TenantProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
@@ -13,12 +20,30 @@ import org.springframework.context.annotation.Profile;
|
||||
*/
|
||||
@Import(DateMetaObjectHandler.class)
|
||||
public class DefaultMybatisPlusConfig {
|
||||
@Autowired
|
||||
private TenantHandler tenantHandler;
|
||||
|
||||
@Autowired
|
||||
private ISqlParserFilter sqlParserFilter;
|
||||
|
||||
@Autowired
|
||||
private TenantProperties tenantProperties;
|
||||
|
||||
/**
|
||||
* 分页插件,自动识别数据库类型
|
||||
*/
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
return new PaginationInterceptor();
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
boolean enableTenant = tenantProperties.getEnable();
|
||||
//是否开启多租户隔离
|
||||
if (enableTenant) {
|
||||
TenantSqlParser tenantSqlParser = new TenantSqlParser()
|
||||
.setTenantHandler(tenantHandler);
|
||||
paginationInterceptor.setSqlParserList(CollUtil.toList(tenantSqlParser));
|
||||
paginationInterceptor.setSqlParserFilter(sqlParserFilter);
|
||||
}
|
||||
return paginationInterceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.central.db.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
|
||||
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
|
||||
import com.central.common.utils.TenantContextHolder;
|
||||
import com.central.common.properties.TenantProperties;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.NullValue;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* 多租户自动配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
@EnableConfigurationProperties(TenantProperties.class)
|
||||
public class TenantAutoConfigure {
|
||||
@Autowired
|
||||
private TenantProperties tenantProperties;
|
||||
|
||||
@Bean
|
||||
public TenantHandler tenantHandler() {
|
||||
return new TenantHandler() {
|
||||
/**
|
||||
* 获取租户id
|
||||
*/
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
String tenant = TenantContextHolder.getTenant();
|
||||
if (tenant != null) {
|
||||
return new StringValue(TenantContextHolder.getTenant());
|
||||
}
|
||||
return new NullValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取租户列名
|
||||
*/
|
||||
@Override
|
||||
public String getTenantIdColumn() {
|
||||
return "tenant_id";
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤不需要根据租户隔离的表
|
||||
* @param tableName 表名
|
||||
*/
|
||||
@Override
|
||||
public boolean doTableFilter(String tableName) {
|
||||
return tenantProperties.getIgnoreTables().stream().anyMatch(
|
||||
(e) -> e.equalsIgnoreCase(tableName)
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤不需要根据租户隔离的MappedStatement
|
||||
*/
|
||||
@Bean
|
||||
public ISqlParserFilter sqlParserFilter() {
|
||||
return metaObject -> {
|
||||
MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
|
||||
return tenantProperties.getIgnoreSqls().stream().anyMatch(
|
||||
(e) -> e.equalsIgnoreCase(ms.getId())
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.db.config.TenantAutoConfigure
|
||||
+9
-6
@@ -3,6 +3,7 @@ package com.central.common.ribbon.config;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.utils.TenantContextHolder;
|
||||
import feign.RequestInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
@@ -28,14 +29,16 @@ public class FeignInterceptorConfig {
|
||||
.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
|
||||
//传递access_token,无网关隔离时需要传递
|
||||
/*String token = extractHeaderToken(request);
|
||||
//传递access_token,无网络隔离时需要传递
|
||||
/*
|
||||
String token = extractHeaderToken(request);
|
||||
if (StrUtil.isEmpty(token)) {
|
||||
token = request.getParameter(CommonConstant.ACCESS_TOKEN);
|
||||
}
|
||||
if (StrUtil.isNotEmpty(token)) {
|
||||
template.header(CommonConstant.TOKEN_HEADER, CommonConstant.BEARER_TYPE + " " + token);
|
||||
}*/
|
||||
}
|
||||
*/
|
||||
|
||||
//传递username
|
||||
String username = request.getHeader(SecurityConstants.USER_HEADER);
|
||||
@@ -50,9 +53,9 @@ public class FeignInterceptorConfig {
|
||||
}
|
||||
|
||||
//传递client
|
||||
String client = request.getHeader(SecurityConstants.CLIENT_HEADER);
|
||||
if (StrUtil.isNotEmpty(client)) {
|
||||
template.header(SecurityConstants.CLIENT_HEADER, client);
|
||||
String tenant = TenantContextHolder.getTenant();
|
||||
if (StrUtil.isNotEmpty(tenant)) {
|
||||
template.header(SecurityConstants.TENANT_HEADER, tenant);
|
||||
}
|
||||
};
|
||||
return requestInterceptor;
|
||||
|
||||
Reference in New Issue
Block a user