From 08d4493994a649b725d94e917b31d42c930284ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Mon, 15 Jul 2024 15:18:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20bug=20?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitee/ISSUE_TEMPLATE.zh-CN.md | 49 ---------------------------------- .gitee/ISSUE_TEMPLATE/bug.yml | 5 ++-- 2 files changed, 3 insertions(+), 51 deletions(-) delete mode 100644 .gitee/ISSUE_TEMPLATE.zh-CN.md diff --git a/.gitee/ISSUE_TEMPLATE.zh-CN.md b/.gitee/ISSUE_TEMPLATE.zh-CN.md deleted file mode 100644 index cf82237f3..000000000 --- a/.gitee/ISSUE_TEMPLATE.zh-CN.md +++ /dev/null @@ -1,49 +0,0 @@ -### 使用版本(未按照模板填写直接删除) - -- jdk版本(带上尾号): 例如 1.8.0_202 -- 框架版本(项目启动时输出的版本号): 例如 4.4.0 -- 其他依赖版本(你觉得有必要的): - -### 问题前提 - -> 功能不好用 不会用 是否已经看过项目文档 -> 项目运行报错 是否已经拿着报错信息去百度 常见报错百度百度足以 -> 是否搜索过其他issue 一些已经解决的问题 会在issue内留下解决方法 -> 无法线上解决或者与框架无关的问题的欢迎加VIP群跟作者一对一谈 - -### 异常模块 - -> 此报错都涉及到那些系统模块 - -例如 ruoyi-system ruoyi-auth 等等 - -### 问题描述 - -> 越详细越容易直击问题所在 - -已知: XXX功能不好用 或 XXX数据不正常 等等 - -### 希望结果 - -> 想知道你觉得怎么样是正常或者合理的 - -希望功能可以有XXX结果 或者 XXX现象 - -### 重现步骤 - -> 作者并不知道这个问题是如何出现的 - -- 1 -- 2 -- 3 - -### 相关代码与报错信息(请勿发混乱格式) - -> 代码可按照如下形式提供或者截图均可 越详细越好 -> 大多数问题都是 代码编写错误问题 逻辑问题 或者用法错误等问题 - -```java -public class XXX { - -} -``` \ No newline at end of file diff --git a/.gitee/ISSUE_TEMPLATE/bug.yml b/.gitee/ISSUE_TEMPLATE/bug.yml index 8a5d06500..045001336 100644 --- a/.gitee/ISSUE_TEMPLATE/bug.yml +++ b/.gitee/ISSUE_TEMPLATE/bug.yml @@ -9,8 +9,9 @@ body: label: 版本 description: 你当前正在使用我们软件的哪个版本(pom文件内的版本号)? value: | - jdk版本(带上尾号): 例如 17.0.8 - 框架版本(项目启动时输出的版本号): 例如 5.1.1 + 注意: 未填写版本号不予处理直接关闭或删除 + jdk版本(带上尾号): + 框架版本(项目启动时输出的版本号): 其他依赖版本(你觉得有必要的): validations: required: true From 32743d8a76ae827d1940ca503079f185b3f7896e Mon Sep 17 00:00:00 2001 From: amadeus5201 <1491459939@qq.com> Date: Fri, 16 Aug 2024 17:29:23 +0800 Subject: [PATCH 2/4] =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/helper/DataPermissionHelper.java | 45 ++++++++++++++++++- .../common/tenant/helper/TenantHelper.java | 45 ++++++++++++++++++- 2 files changed, 86 insertions(+), 4 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 2afe9ee47..fe0c224fa 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 @@ -2,14 +2,17 @@ package org.dromara.common.mybatis.helper; import cn.dev33.satoken.context.SaHolder; import cn.dev33.satoken.context.model.SaStorage; +import cn.hutool.core.collection.CollectionUtil; 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 org.dromara.common.core.utils.reflect.ReflectUtils; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.function.Supplier; /** @@ -24,6 +27,8 @@ public class DataPermissionHelper { private static final String DATA_PERMISSION_KEY = "data:permission"; + private static final ThreadLocal REENTRANT_IGNORE_PERMISSION = new ThreadLocal<>(); + /** * 从上下文中获取指定键的变量值,并将其转换为指定的类型 * @@ -70,14 +75,50 @@ public class DataPermissionHelper { * 开启忽略数据权限(开启后需手动调用 {@link #disableIgnore()} 关闭) */ public static void enableIgnore() { - InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build()); + 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 { + InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build()); + REENTRANT_IGNORE_PERMISSION.set(1); + } } /** * 关闭忽略数据权限 */ public static void disableIgnore() { - InterceptorIgnoreHelper.clearIgnoreStrategy(); + 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) { + InterceptorIgnoreHelper.clearIgnoreStrategy(); + } + } else { + if (!hasCount) { + if (count.compareTo(1) == 0) { + ignoreStrategy.setDataPermission(null); + REENTRANT_IGNORE_PERMISSION.remove(); + } + } + } + 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 9d087e113..64de20c23 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 @@ -1,6 +1,7 @@ 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 com.alibaba.ttl.TransmittableThreadLocal; import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy; @@ -11,9 +12,11 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.common.core.constant.GlobalConstants; 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.Objects; import java.util.function.Supplier; /** @@ -29,6 +32,8 @@ public class TenantHelper { private static final ThreadLocal TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>(); + private static final ThreadLocal REENTRANT_IGNORE_TENANT = new ThreadLocal<>(); + /** * 租户功能是否启用 */ @@ -40,14 +45,50 @@ public class TenantHelper { * 开启忽略租户(开启后需手动调用 {@link #disableIgnore()} 关闭) */ public static void enableIgnore() { - InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); + 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 { + InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); + REENTRANT_IGNORE_TENANT.set(1); + } } /** * 关闭忽略租户 */ public static void disableIgnore() { - InterceptorIgnoreHelper.clearIgnoreStrategy(); + 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) { + InterceptorIgnoreHelper.clearIgnoreStrategy(); + } + } else { + if (!hasCount) { + if (count.compareTo(1) == 0) { + ignoreStrategy.setTenantLine(null); + REENTRANT_IGNORE_TENANT.remove(); + } + } + } + if (hasCount) { + REENTRANT_IGNORE_TENANT.set(count - 1); + } + } else { + InterceptorIgnoreHelper.clearIgnoreStrategy(); + REENTRANT_IGNORE_TENANT.remove(); + } } /** 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 3/4] =?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(); } } From bedf08b1d55ff32efcce285f72048d4c25548329 Mon Sep 17 00:00:00 2001 From: amadeus5201 <1491459939@qq.com> Date: Tue, 20 Aug 2024 17:17:45 +0800 Subject: [PATCH 4/4] =?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=85=A5=E5=BF=BD?= =?UTF-8?q?=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/helper/DataPermissionHelper.java | 57 +++++++++---------- .../common/tenant/helper/TenantHelper.java | 57 ++++++++++--------- 2 files changed, 56 insertions(+), 58 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 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(); } } }