diff --git a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/helper/DataPermissionHelper.java b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/helper/DataPermissionHelper.java index 0484684cd..8f67a937e 100644 --- a/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/helper/DataPermissionHelper.java +++ b/ruoyi-common/ruoyi-common-mybatis/src/main/java/org/dromara/common/mybatis/helper/DataPermissionHelper.java @@ -10,10 +10,7 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.dromara.common.core.utils.reflect.ReflectUtils; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.Stack; +import java.util.*; import java.util.function.Supplier; /** @@ -28,8 +25,23 @@ public class DataPermissionHelper { private static final String DATA_PERMISSION_KEY = "data:permission"; - private static final Stack REENTRANT_IGNORE_PERMISSION_STACK = new Stack<>(); + private static final ThreadLocal> REENTRANT_IGNORE_PERMISSION = new ThreadLocal<>(); + private static Stack getReentrantIgnorePermissionStack() { + return Optional.ofNullable(REENTRANT_IGNORE_PERMISSION.get()).orElseGet(() -> { + REENTRANT_IGNORE_PERMISSION.set(new Stack<>()); + return REENTRANT_IGNORE_PERMISSION.get(); + }); + } + + private static void clearReentrantIgnore() { + REENTRANT_IGNORE_PERMISSION.remove(); + } + + private static void reentrantIgnoreIncrement() { + Stack reentrantStack = getReentrantIgnorePermissionStack(); + reentrantStack.push(reentrantStack.size() + 1); + } /** * 从上下文中获取指定键的变量值,并将其转换为指定的类型 * @@ -80,18 +92,12 @@ public class DataPermissionHelper { IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get(); if (ignoreStrategy == null) { InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build()); - REENTRANT_IGNORE_PERMISSION_STACK.push(1); } else { - if (Boolean.TRUE.equals(ignoreStrategy.getDataPermission())) { - if (REENTRANT_IGNORE_PERMISSION_STACK.isEmpty()) { - throw new IllegalStateException("ignore data permission error"); - } - REENTRANT_IGNORE_PERMISSION_STACK.push(REENTRANT_IGNORE_PERMISSION_STACK.peek() + 1); - } else { + if (!Boolean.TRUE.equals(ignoreStrategy.getDataPermission())) { ignoreStrategy.setDataPermission(true); - REENTRANT_IGNORE_PERMISSION_STACK.push(1); } } + reentrantIgnoreIncrement(); } /** @@ -101,28 +107,19 @@ public class DataPermissionHelper { ThreadLocal IGNORE_STRATEGY_LOCAL = (ThreadLocal) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL")); IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get(); if (ignoreStrategy != null) { - boolean empty = REENTRANT_IGNORE_PERMISSION_STACK.isEmpty(); - int pop = empty ? 1 : REENTRANT_IGNORE_PERMISSION_STACK.pop(); - boolean shouldClear = !Boolean.TRUE.equals(ignoreStrategy.getTenantLine()) + boolean noOtherIgnoreStrategy = !Boolean.TRUE.equals(ignoreStrategy.getTenantLine()) && !Boolean.TRUE.equals(ignoreStrategy.getDynamicTableName()) && !Boolean.TRUE.equals(ignoreStrategy.getBlockAttack()) && !Boolean.TRUE.equals(ignoreStrategy.getIllegalSql()) && CollectionUtil.isEmpty(ignoreStrategy.getOthers()); - if (shouldClear) { - if (empty) { - InterceptorIgnoreHelper.clearIgnoreStrategy(); - } else { - if (pop == 1) { - InterceptorIgnoreHelper.clearIgnoreStrategy(); - } - } - } else { - if (empty) { + Stack reentrantStack = getReentrantIgnorePermissionStack(); + boolean empty = reentrantStack.isEmpty() || reentrantStack.pop() == 1; + if (noOtherIgnoreStrategy && empty) { + InterceptorIgnoreHelper.clearIgnoreStrategy(); + } else if (empty) { + if (Boolean.TRUE.equals(ignoreStrategy.getDataPermission())) { ignoreStrategy.setDataPermission(false); - } else { - if (pop == 1) { - ignoreStrategy.setDataPermission(false); - } + clearReentrantIgnore(); } } } diff --git a/ruoyi-common/ruoyi-common-tenant/src/main/java/org/dromara/common/tenant/helper/TenantHelper.java b/ruoyi-common/ruoyi-common-tenant/src/main/java/org/dromara/common/tenant/helper/TenantHelper.java index 3eaa6b792..a312411e0 100644 --- a/ruoyi-common/ruoyi-common-tenant/src/main/java/org/dromara/common/tenant/helper/TenantHelper.java +++ b/ruoyi-common/ruoyi-common-tenant/src/main/java/org/dromara/common/tenant/helper/TenantHelper.java @@ -16,6 +16,7 @@ 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.Optional; import java.util.Stack; import java.util.function.Supplier; @@ -32,8 +33,24 @@ public class TenantHelper { private static final ThreadLocal TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>(); - private static final Stack REENTRANT_IGNORE_TENANT_STACK = new Stack<>(); + private static final ThreadLocal> REENTRANT_IGNORE_TENANT = new ThreadLocal<>(); + + private static Stack getReentrantIgnoreTenantStack() { + return Optional.ofNullable(REENTRANT_IGNORE_TENANT.get()).orElseGet(() -> { + REENTRANT_IGNORE_TENANT.set(new Stack<>()); + return REENTRANT_IGNORE_TENANT.get(); + }); + } + + private static void clearReentrantIgnore() { + REENTRANT_IGNORE_TENANT.remove(); + } + + private static void reentrantIgnoreIncrement() { + Stack reentrantStack = getReentrantIgnoreTenantStack(); + reentrantStack.push(reentrantStack.size() + 1); + } /** * 租户功能是否启用 */ @@ -49,18 +66,12 @@ public class TenantHelper { IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get(); if (ignoreStrategy == null) { InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); - REENTRANT_IGNORE_TENANT_STACK.push(1); } else { - if (Boolean.TRUE.equals(ignoreStrategy.getTenantLine())) { - if (REENTRANT_IGNORE_TENANT_STACK.isEmpty()) { - throw new IllegalStateException("ignore tenant error"); - } - REENTRANT_IGNORE_TENANT_STACK.push(REENTRANT_IGNORE_TENANT_STACK.peek() + 1); - } else { + if (!Boolean.TRUE.equals(ignoreStrategy.getTenantLine())) { ignoreStrategy.setTenantLine(true); - REENTRANT_IGNORE_TENANT_STACK.push(1); } } + reentrantIgnoreIncrement(); } /** @@ -70,29 +81,19 @@ public class TenantHelper { ThreadLocal IGNORE_STRATEGY_LOCAL = (ThreadLocal) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL")); IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get(); if (ignoreStrategy != null) { - boolean empty = REENTRANT_IGNORE_TENANT_STACK.isEmpty(); - int pop = empty ? 1 : REENTRANT_IGNORE_TENANT_STACK.pop(); - boolean shouldClear = !Boolean.TRUE.equals(ignoreStrategy.getDynamicTableName()) + 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()); - if (shouldClear) { - if (empty) { - InterceptorIgnoreHelper.clearIgnoreStrategy(); - } else { - if (pop == 1) { - InterceptorIgnoreHelper.clearIgnoreStrategy(); - } - } - } else { - if (empty) { - ignoreStrategy.setTenantLine(false); - } else { - if (pop == 1) { - ignoreStrategy.setTenantLine(false); - } - } + Stack reentrantStack = getReentrantIgnoreTenantStack(); + boolean empty = reentrantStack.isEmpty() || reentrantStack.pop() == 1; + if (noOtherIgnoreStrategy && empty) { + InterceptorIgnoreHelper.clearIgnoreStrategy(); + clearReentrantIgnore(); + } else if (empty) { + ignoreStrategy.setDataPermission(false); + clearReentrantIgnore(); } } }