From 2c27611473c043fef3b44962cb27881e1f72c386 Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Sun, 30 Apr 2023 01:18:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AA=E6=8C=87=E5=AE=9A=E5=85=B7=E4=BD=93?= =?UTF-8?q?=E8=A6=81=E6=A0=A1=E9=AA=8C=E7=9A=84=E6=9D=83=E9=99=90=E7=A0=81?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E7=9B=B4=E6=8E=A5=E8=B7=B3=E8=BF=87=20close?= =?UTF-8?q?=20#I6ZE9Z?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/dev33/satoken/stp/StpLogic.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java index 6197e7b5..42ae2efa 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java @@ -1382,7 +1382,13 @@ public class StpLogic { * @param roleArray 角色标识数组 */ public void checkRoleAnd(String... roleArray){ - Object loginId = getLoginId(); + // 如果没有指定权限,那么直接跳过 + if(roleArray == null || roleArray.length == 0) { + return; + } + + // 开始校验 + Object loginId = getLoginId(); List roleList = getRoleList(loginId); for (String role : roleArray) { if(!hasElement(roleList, role)) { @@ -1396,6 +1402,12 @@ public class StpLogic { * @param roleArray 角色标识数组 */ public void checkRoleOr(String... roleArray){ + // 如果没有指定权限,那么直接跳过 + if(roleArray == null || roleArray.length == 0) { + return; + } + + // 开始校验 Object loginId = getLoginId(); List roleList = getRoleList(loginId); for (String role : roleArray) { @@ -1404,9 +1416,9 @@ public class StpLogic { return; } } - if(roleArray.length > 0) { - throw new NotRoleException(roleArray[0], this.loginType).setCode(SaErrorCode.CODE_11041); - } + + // 代码至此,说明一个都没通过,需要抛出无角色异常 + throw new NotRoleException(roleArray[0], this.loginType).setCode(SaErrorCode.CODE_11041); } @@ -1495,6 +1507,12 @@ public class StpLogic { * @param permissionArray 权限码数组 */ public void checkPermissionAnd(String... permissionArray){ + // 如果没有指定权限,那么直接跳过 + if(permissionArray == null || permissionArray.length == 0) { + return; + } + + // 开始校验 Object loginId = getLoginId(); List permissionList = getPermissionList(loginId); for (String permission : permissionArray) { @@ -1509,6 +1527,12 @@ public class StpLogic { * @param permissionArray 权限码数组 */ public void checkPermissionOr(String... permissionArray){ + // 如果没有指定权限,那么直接跳过 + if(permissionArray == null || permissionArray.length == 0) { + return; + } + + // 开始校验 Object loginId = getLoginId(); List permissionList = getPermissionList(loginId); for (String permission : permissionArray) { @@ -1517,9 +1541,9 @@ public class StpLogic { return; } } - if(permissionArray.length > 0) { - throw new NotPermissionException(permissionArray[0], this.loginType).setCode(SaErrorCode.CODE_11051); - } + + // 代码至此,说明一个都没通过,需要抛出无权限异常 + throw new NotPermissionException(permissionArray[0], this.loginType).setCode(SaErrorCode.CODE_11051); }