From 28a359836a9b5784db0c9fbefb448f58bdbd2393 Mon Sep 17 00:00:00 2001 From: amadeus5201 <1491459939@qq.com> Date: Sat, 17 Aug 2024 14:05:55 +0800 Subject: [PATCH] =?UTF-8?q?fix=3D>=E6=9D=83=E9=99=90=E5=92=8C=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E5=8F=AF=E4=BB=A5=E5=90=8C=E6=97=B6=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E5=8F=AF=E9=87=8D=E5=A4=8D=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=EF=BC=8C=E6=94=B9=E7=94=A8=E6=A0=88=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/helper/DataPermissionHelper.java | 59 ++++++++++-------- .../common/tenant/helper/TenantHelper.java | 60 ++++++++++--------- 2 files changed, 66 insertions(+), 53 deletions(-) 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 fe0c224fa..0484684cd 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 @@ -13,6 +13,7 @@ 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.function.Supplier; /** @@ -27,7 +28,7 @@ public class DataPermissionHelper { private static final String DATA_PERMISSION_KEY = "data:permission"; - private static final ThreadLocal REENTRANT_IGNORE_PERMISSION = new ThreadLocal<>(); + private static final Stack REENTRANT_IGNORE_PERMISSION_STACK = new Stack<>(); /** * 从上下文中获取指定键的变量值,并将其转换为指定的类型 @@ -77,17 +78,19 @@ public class DataPermissionHelper { public static void enableIgnore() { ThreadLocal IGNORE_STRATEGY_LOCAL = (ThreadLocal) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL")); IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get(); - if (Objects.nonNull(ignoreStrategy)) { - if (!Boolean.TRUE.equals(ignoreStrategy.getDataPermission())) { - ignoreStrategy.setDataPermission(true); - REENTRANT_IGNORE_PERMISSION.set(1); - } else { - Integer count = REENTRANT_IGNORE_PERMISSION.get(); - REENTRANT_IGNORE_PERMISSION.set(count + 1); - } - } else { + if (ignoreStrategy == null) { InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build()); - REENTRANT_IGNORE_PERMISSION.set(1); + 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 { + ignoreStrategy.setDataPermission(true); + REENTRANT_IGNORE_PERMISSION_STACK.push(1); + } } } @@ -97,27 +100,31 @@ public class DataPermissionHelper { public static void disableIgnore() { ThreadLocal IGNORE_STRATEGY_LOCAL = (ThreadLocal) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL")); IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get(); - if (Objects.nonNull(ignoreStrategy)) { - Integer count = REENTRANT_IGNORE_PERMISSION.get(); - boolean hasCount = count.compareTo(1) > 0; - if (ignoreStrategy.getBlockAttack() == null && ignoreStrategy.getDynamicTableName() == null && ignoreStrategy.getTenantLine() == null && ignoreStrategy.getIllegalSql() == null && CollectionUtil.isEmpty(ignoreStrategy.getOthers())) { - if (!hasCount) { + 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.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 (!hasCount) { - if (count.compareTo(1) == 0) { - ignoreStrategy.setDataPermission(null); - REENTRANT_IGNORE_PERMISSION.remove(); + if (empty) { + ignoreStrategy.setDataPermission(false); + } else { + if (pop == 1) { + ignoreStrategy.setDataPermission(false); } } } - if (hasCount) { - REENTRANT_IGNORE_PERMISSION.set(count - 1); - } - } else { - InterceptorIgnoreHelper.clearIgnoreStrategy(); - REENTRANT_IGNORE_PERMISSION.remove(); } } 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 64de20c23..3eaa6b792 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,7 +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.Objects; +import java.util.Stack; import java.util.function.Supplier; /** @@ -32,7 +32,7 @@ public class TenantHelper { private static final ThreadLocal TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>(); - private static final ThreadLocal REENTRANT_IGNORE_TENANT = new ThreadLocal<>(); + private static final Stack REENTRANT_IGNORE_TENANT_STACK = new Stack<>(); /** * 租户功能是否启用 @@ -47,17 +47,19 @@ public class TenantHelper { public static void enableIgnore() { ThreadLocal IGNORE_STRATEGY_LOCAL = (ThreadLocal) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL")); IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get(); - if (Objects.nonNull(ignoreStrategy)) { - if (!Boolean.TRUE.equals(ignoreStrategy.getTenantLine())) { - ignoreStrategy.setTenantLine(true); - REENTRANT_IGNORE_TENANT.set(1); - } else { - Integer count = REENTRANT_IGNORE_TENANT.get(); - REENTRANT_IGNORE_TENANT.set(count + 1); - } - } else { + if (ignoreStrategy == null) { InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); - REENTRANT_IGNORE_TENANT.set(1); + 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 { + ignoreStrategy.setTenantLine(true); + REENTRANT_IGNORE_TENANT_STACK.push(1); + } } } @@ -67,27 +69,31 @@ public class TenantHelper { public static void disableIgnore() { ThreadLocal IGNORE_STRATEGY_LOCAL = (ThreadLocal) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL")); IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get(); - if (Objects.nonNull(ignoreStrategy)) { - Integer count = REENTRANT_IGNORE_TENANT.get(); - boolean hasCount = count.compareTo(1) > 0; - if (ignoreStrategy.getBlockAttack() == null && ignoreStrategy.getDynamicTableName() == null && ignoreStrategy.getIllegalSql() == null && ignoreStrategy.getDataPermission() == null && CollectionUtil.isEmpty(ignoreStrategy.getOthers())) { - if (!hasCount) { + 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.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 (!hasCount) { - if (count.compareTo(1) == 0) { - ignoreStrategy.setTenantLine(null); - REENTRANT_IGNORE_TENANT.remove(); + if (empty) { + ignoreStrategy.setTenantLine(false); + } else { + if (pop == 1) { + ignoreStrategy.setTenantLine(false); } } } - if (hasCount) { - REENTRANT_IGNORE_TENANT.set(count - 1); - } - } else { - InterceptorIgnoreHelper.clearIgnoreStrategy(); - REENTRANT_IGNORE_TENANT.remove(); } }