mirror of https://gitee.com/stylefeng/roses
【7.3.2】增加一种租户编码的获取方式
parent
52a1f9c44f
commit
24b0331f37
|
@ -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<>();
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package cn.stylefeng.roses.kernel.rule.tenant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 租户编码信息暂存,一般给缓存使用
|
||||||
|
* <p>
|
||||||
|
* 在一次Http请求中生效,一般用在登录接口,当前Context没有LoginUser时使用
|
||||||
|
*
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue