增加多租户功能(应用隔离)
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user