| | |
| | | package org.dromara.common.tenant.helper; |
| | | |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy; |
| | | import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.core.context.ThreadLocalHolder; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.core.utils.reflect.ReflectUtils; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | |
| | | import java.util.Stack; |
| | | import java.util.function.Supplier; |
| | | |
| | | /** |
| | |
| | | |
| | | private static final String DYNAMIC_TENANT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "dynamicTenant"; |
| | | |
| | | private static final String TENANT_ID_KEY = "tempDynamicTenant"; |
| | | private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new ThreadLocal<>(); |
| | | |
| | | private static final ThreadLocal<Stack<Integer>> REENTRANT_IGNORE = ThreadLocal.withInitial(Stack::new); |
| | | |
| | | /** |
| | | * 租户功能是否启用 |
| | |
| | | return Convert.toBool(SpringUtils.getProperty("tenant.enable"), false); |
| | | } |
| | | |
| | | private static IgnoreStrategy getIgnoreStrategy() { |
| | | Object ignoreStrategyLocal = ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL")); |
| | | if (ignoreStrategyLocal instanceof ThreadLocal<?> IGNORE_STRATEGY_LOCAL) { |
| | | if (IGNORE_STRATEGY_LOCAL.get() instanceof IgnoreStrategy ignoreStrategy) { |
| | | return ignoreStrategy; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 开启忽略租户(开启后需手动调用 {@link #disableIgnore()} 关闭) |
| | | */ |
| | | public static void enableIgnore() { |
| | | InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); |
| | | IgnoreStrategy ignoreStrategy = getIgnoreStrategy(); |
| | | if (ObjectUtil.isNull(ignoreStrategy)) { |
| | | InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); |
| | | } else { |
| | | ignoreStrategy.setTenantLine(true); |
| | | } |
| | | Stack<Integer> reentrantStack = REENTRANT_IGNORE.get(); |
| | | reentrantStack.push(reentrantStack.size() + 1); |
| | | } |
| | | |
| | | /** |
| | | * 关闭忽略租户 |
| | | */ |
| | | public static void disableIgnore() { |
| | | InterceptorIgnoreHelper.clearIgnoreStrategy(); |
| | | IgnoreStrategy ignoreStrategy = getIgnoreStrategy(); |
| | | if (ObjectUtil.isNotNull(ignoreStrategy)) { |
| | | boolean noOtherIgnoreStrategy = !Boolean.TRUE.equals(ignoreStrategy.getDynamicTableName()) |
| | | && !Boolean.TRUE.equals(ignoreStrategy.getBlockAttack()) |
| | | && !Boolean.TRUE.equals(ignoreStrategy.getIllegalSql()) |
| | | && !Boolean.TRUE.equals(ignoreStrategy.getDataPermission()) |
| | | && CollectionUtil.isEmpty(ignoreStrategy.getOthers()); |
| | | Stack<Integer> reentrantStack = REENTRANT_IGNORE.get(); |
| | | boolean empty = reentrantStack.isEmpty() || reentrantStack.pop() == 1; |
| | | if (noOtherIgnoreStrategy && empty) { |
| | | InterceptorIgnoreHelper.clearIgnoreStrategy(); |
| | | } else if (empty) { |
| | | ignoreStrategy.setTenantLine(false); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | public static void setDynamic(String tenantId) { |
| | | setDynamic(tenantId, false); |
| | | } |
| | | |
| | | /** |
| | | * 设置动态租户(一直有效 需要手动清理) |
| | | * <p> |
| | | * 如果为未登录状态下 那么只在当前线程内生效 |
| | | * |
| | | * @param tenantId 租户id |
| | | * @param global 是否全局生效 |
| | | */ |
| | | public static void setDynamic(String tenantId) { |
| | | public static void setDynamic(String tenantId, boolean global) { |
| | | if (!isEnable()) { |
| | | return; |
| | | } |
| | | if (!isLogin()) { |
| | | ThreadLocalHolder.set(TENANT_ID_KEY, tenantId); |
| | | if (!isLogin() || !global) { |
| | | TEMP_DYNAMIC_TENANT.set(tenantId); |
| | | return; |
| | | } |
| | | String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); |
| | | RedisUtils.setCacheObject(cacheKey, tenantId); |
| | | ThreadLocalHolder.set(cacheKey, tenantId); |
| | | } |
| | | |
| | | /** |
| | |
| | | return null; |
| | | } |
| | | if (!isLogin()) { |
| | | return ThreadLocalHolder.get(TENANT_ID_KEY); |
| | | return TEMP_DYNAMIC_TENANT.get(); |
| | | } |
| | | String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); |
| | | String tenantId = ThreadLocalHolder.get(cacheKey); |
| | | // 如果线程内有值 优先返回 |
| | | String tenantId = TEMP_DYNAMIC_TENANT.get(); |
| | | if (StringUtils.isNotBlank(tenantId)) { |
| | | return tenantId; |
| | | } |
| | | String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); |
| | | tenantId = RedisUtils.getCacheObject(cacheKey); |
| | | ThreadLocalHolder.set(cacheKey, tenantId); |
| | | return tenantId; |
| | | } |
| | | |
| | |
| | | return; |
| | | } |
| | | if (!isLogin()) { |
| | | ThreadLocalHolder.remove(TENANT_ID_KEY); |
| | | TEMP_DYNAMIC_TENANT.remove(); |
| | | return; |
| | | } |
| | | TEMP_DYNAMIC_TENANT.remove(); |
| | | String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); |
| | | RedisUtils.deleteObject(cacheKey); |
| | | ThreadLocalHolder.remove(cacheKey); |
| | | } |
| | | |
| | | /** |