diff --git a/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/TenantCodeHolder.java b/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/OnceTenantCodeHolder.java similarity index 90% rename from kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/TenantCodeHolder.java rename to kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/OnceTenantCodeHolder.java index dbee09698..2615cdca5 100644 --- a/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/TenantCodeHolder.java +++ b/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/OnceTenantCodeHolder.java @@ -2,11 +2,13 @@ package cn.stylefeng.roses.kernel.rule.tenant; /** * 租户编码信息暂存,一般给缓存使用 + *

+ * 一次性使用,使用完就立即释放 * * @author fengshuonan * @date 2022/11/8 0:58 */ -public class TenantCodeHolder { +public class OnceTenantCodeHolder { private static final ThreadLocal CONTEXT_HOLDER = new ThreadLocal<>(); diff --git a/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/RequestTenantCodeHolder.java b/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/RequestTenantCodeHolder.java new file mode 100644 index 000000000..4e50c4832 --- /dev/null +++ b/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/tenant/RequestTenantCodeHolder.java @@ -0,0 +1,45 @@ +package cn.stylefeng.roses.kernel.rule.tenant; + +/** + * 租户编码信息暂存,一般给缓存使用 + *

+ * 在一次Http请求中生效,一般用在登录接口,当前Context没有LoginUser时使用 + * + * @author fengshuonan + * @date 2022/11/8 0:58 + */ +public class RequestTenantCodeHolder { + + private static final ThreadLocal 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(); + } + +} diff --git a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/tenant/TenantCacheProxy.java b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/tenant/TenantCacheProxy.java index 270721017..e864310c7 100644 --- a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/tenant/TenantCacheProxy.java +++ b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/tenant/TenantCacheProxy.java @@ -1,7 +1,7 @@ package cn.stylefeng.roses.kernel.cache.api.tenant; 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.Method; @@ -45,13 +45,13 @@ public class TenantCacheProxy implements InvocationHandler { Object result = null; try { // 设置租户编码 - TenantCodeHolder.setTenantCode(tenantCode); + OnceTenantCodeHolder.setTenantCode(tenantCode); // 执行原有缓存操作类 result = method.invoke(targetCacheObject, args); } finally { // 清除租户编码 - TenantCodeHolder.clearTenantCode(); + OnceTenantCodeHolder.clearTenantCode(); } return result; } diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/TenantPrefixService.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/TenantPrefixService.java index 1065be843..49f6bc45a 100644 --- a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/TenantPrefixService.java +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/TenantPrefixService.java @@ -3,7 +3,8 @@ package cn.stylefeng.roses.kernel.system.modular.user.service; import cn.hutool.core.util.ObjectUtil; import cn.stylefeng.roses.kernel.auth.api.context.LoginContext; 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 org.springframework.stereotype.Service; @@ -19,15 +20,21 @@ public class TenantPrefixService implements TenantPrefixApi { @Override public String getTenantPrefix() { - // 先从ThreadLocal中获取租户缓存编码 - String tenantCode = TenantCodeHolder.getTenantCode(); + // 1. 优先从OnceTenantCodeHolder中获取租户缓存编码 + String tenantCode = OnceTenantCodeHolder.getTenantCode(); // 如果有则以ThreadLocal中为准 if (ObjectUtil.isNotEmpty(tenantCode)) { return tenantCode; } - // 之后,从LoginUser中获取租户编码 + // 2. 次之,从RequestTenantCodeHolder中获取租户缓存编码,一般用在登录接口时,系统还没有LoginUser + String requestHttpContextTenantCode = RequestTenantCodeHolder.getTenantCode(); + if (ObjectUtil.isNotEmpty(requestHttpContextTenantCode)) { + return requestHttpContextTenantCode; + } + + // 3. 最后,从LoginUser中获取租户编码 LoginUser loginUser = LoginContext.me().getLoginUserNullable(); if (loginUser == null) {