【7.3.2】增加一种租户编码的获取方式

pull/41/head
fengshuonan 2022-11-09 19:58:05 +08:00
parent 52a1f9c44f
commit 24b0331f37
4 changed files with 62 additions and 8 deletions

View File

@ -2,11 +2,13 @@ package cn.stylefeng.roses.kernel.rule.tenant;
/** /**
* 使 * 使
* <p>
* 使使
* *
* @author fengshuonan * @author fengshuonan
* @date 2022/11/8 0:58 * @date 2022/11/8 0:58
*/ */
public class TenantCodeHolder { public class OnceTenantCodeHolder {
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>(); private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();

View File

@ -0,0 +1,45 @@
package cn.stylefeng.roses.kernel.rule.tenant;
/**
* 使
* <p>
* HttpContextLoginUser使
*
* @author fengshuonan
* @date 2022/11/8 0:58
*/
public class RequestTenantCodeHolder {
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
/**
*
*
* @author fengshuonan
* @date 2022/11/8 0:59
*/
public static void setTenantCode(String aesKey) {
CONTEXT_HOLDER.set(aesKey);
}
/**
*
*
* @author fengshuonan
* @date 2022/11/8 0:59
*/
public static String getTenantCode() {
return CONTEXT_HOLDER.get();
}
/**
*
*
* @author fengshuonan
* @date 2022/11/8 0:59
*/
public static void clearTenantCode() {
CONTEXT_HOLDER.remove();
}
}

View File

@ -1,7 +1,7 @@
package cn.stylefeng.roses.kernel.cache.api.tenant; package cn.stylefeng.roses.kernel.cache.api.tenant;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.rule.tenant.TenantCodeHolder; import cn.stylefeng.roses.kernel.rule.tenant.OnceTenantCodeHolder;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -45,13 +45,13 @@ public class TenantCacheProxy implements InvocationHandler {
Object result = null; Object result = null;
try { try {
// 设置租户编码 // 设置租户编码
TenantCodeHolder.setTenantCode(tenantCode); OnceTenantCodeHolder.setTenantCode(tenantCode);
// 执行原有缓存操作类 // 执行原有缓存操作类
result = method.invoke(targetCacheObject, args); result = method.invoke(targetCacheObject, args);
} finally { } finally {
// 清除租户编码 // 清除租户编码
TenantCodeHolder.clearTenantCode(); OnceTenantCodeHolder.clearTenantCode();
} }
return result; return result;
} }

View File

@ -3,7 +3,8 @@ package cn.stylefeng.roses.kernel.system.modular.user.service;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext; import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser; import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.rule.tenant.TenantCodeHolder; import cn.stylefeng.roses.kernel.rule.tenant.OnceTenantCodeHolder;
import cn.stylefeng.roses.kernel.rule.tenant.RequestTenantCodeHolder;
import cn.stylefeng.roses.kernel.rule.tenant.TenantPrefixApi; import cn.stylefeng.roses.kernel.rule.tenant.TenantPrefixApi;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -19,15 +20,21 @@ public class TenantPrefixService implements TenantPrefixApi {
@Override @Override
public String getTenantPrefix() { public String getTenantPrefix() {
// 先从ThreadLocal中获取租户缓存编码 // 1. 优先从OnceTenantCodeHolder中获取租户缓存编码
String tenantCode = TenantCodeHolder.getTenantCode(); String tenantCode = OnceTenantCodeHolder.getTenantCode();
// 如果有则以ThreadLocal中为准 // 如果有则以ThreadLocal中为准
if (ObjectUtil.isNotEmpty(tenantCode)) { if (ObjectUtil.isNotEmpty(tenantCode)) {
return tenantCode; return tenantCode;
} }
// 之后从LoginUser中获取租户编码 // 2. 次之从RequestTenantCodeHolder中获取租户缓存编码一般用在登录接口时系统还没有LoginUser
String requestHttpContextTenantCode = RequestTenantCodeHolder.getTenantCode();
if (ObjectUtil.isNotEmpty(requestHttpContextTenantCode)) {
return requestHttpContextTenantCode;
}
// 3. 最后从LoginUser中获取租户编码
LoginUser loginUser = LoginContext.me().getLoginUserNullable(); LoginUser loginUser = LoginContext.me().getLoginUserNullable();
if (loginUser == null) { if (loginUser == null) {