From 953ba965aa677a3eb7a977f4cdb10832629090ea Mon Sep 17 00:00:00 2001 From: xy <3293232930@qq.com> Date: Mon, 24 May 2021 11:24:22 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=A8=8D=E5=BE=AE=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E4=B8=80=E4=B8=8Bcn.dev33.satoken.action.SaTokenActionDefaultI?= =?UTF-8?q?mpl.checkMethodAnnotation=E7=9A=84=E9=AA=8C=E8=AF=81=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action/SaTokenActionDefaultImpl.java | 46 +++++++------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/action/SaTokenActionDefaultImpl.java b/sa-token-core/src/main/java/cn/dev33/satoken/action/SaTokenActionDefaultImpl.java index efa57c5f..39350bb1 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/action/SaTokenActionDefaultImpl.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/action/SaTokenActionDefaultImpl.java @@ -1,5 +1,6 @@ package cn.dev33.satoken.action; +import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.List; import java.util.UUID; @@ -89,44 +90,29 @@ public class SaTokenActionDefaultImpl implements SaTokenAction { // 获取这个 Method 所属的 Class Class clazz = method.getDeclaringClass(); - - - // 从 Class 校验 @SaCheckLogin 注解 - if(clazz.isAnnotationPresent(SaCheckLogin.class)) { - SaCheckLogin at = clazz.getAnnotation(SaCheckLogin.class); + + validateAnnotation(clazz); + validateAnnotation(method); + } + + private void validateAnnotation(AnnotatedElement target) { + // 校验 @SaCheckLogin 注解 + if(target.isAnnotationPresent(SaCheckLogin.class)) { + SaCheckLogin at = target.getAnnotation(SaCheckLogin.class); SaManager.getStpLogic(at.key()).checkByAnnotation(at); } - // 从 Class 校验 @SaCheckRole 注解 - if(clazz.isAnnotationPresent(SaCheckRole.class)) { - SaCheckRole at = clazz.getAnnotation(SaCheckRole.class); + // 校验 @SaCheckRole 注解 + if(target.isAnnotationPresent(SaCheckRole.class)) { + SaCheckRole at = target.getAnnotation(SaCheckRole.class); SaManager.getStpLogic(at.key()).checkByAnnotation(at); } - // 从 Class 校验 @SaCheckPermission 注解 - if(clazz.isAnnotationPresent(SaCheckPermission.class)) { - SaCheckPermission at = clazz.getAnnotation(SaCheckPermission.class); + // 校验 @SaCheckPermission 注解 + if(target.isAnnotationPresent(SaCheckPermission.class)) { + SaCheckPermission at = target.getAnnotation(SaCheckPermission.class); SaManager.getStpLogic(at.key()).checkByAnnotation(at); } - - // 从 Method 校验 @SaCheckLogin 注解 - if(method.isAnnotationPresent(SaCheckLogin.class)) { - SaCheckLogin at = method.getAnnotation(SaCheckLogin.class); - SaManager.getStpLogic(at.key()).checkByAnnotation(at); - } - - // 从 Method 校验 @SaCheckRole 注解 - if(method.isAnnotationPresent(SaCheckRole.class)) { - SaCheckRole at = method.getAnnotation(SaCheckRole.class); - SaManager.getStpLogic(at.key()).checkByAnnotation(at); - } - - // 从 Method 校验 @SaCheckPermission 注解 - if(method.isAnnotationPresent(SaCheckPermission.class)) { - SaCheckPermission at = method.getAnnotation(SaCheckPermission.class); - SaManager.getStpLogic(at.key()).checkByAnnotation(at); - } - } } From ce0879c5b5ced2430616f08992e911ad90eae072 Mon Sep 17 00:00:00 2001 From: xy <3293232930@qq.com> Date: Mon, 24 May 2021 11:46:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BAvalueOf=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E8=8E=B7=E5=8F=96=E5=80=BC,=20=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E4=BA=A7=E7=94=9F=E6=96=B0=E7=9A=84=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../satoken/config/SaTokenConfigFactory.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfigFactory.java b/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfigFactory.java index fb2f1d1e..23ebdd81 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfigFactory.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfigFactory.java @@ -94,9 +94,7 @@ public class SaTokenConfigFactory { Object valueConvert = getObjectByClass(value, field.getType()); field.setAccessible(true); field.set(obj, valueConvert); - } catch (IllegalArgumentException e) { - throw new RuntimeException("属性赋值出错:" + field.getName(), e); - } catch (IllegalAccessException e) { + } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException("属性赋值出错:" + field.getName(), e); } } @@ -112,23 +110,21 @@ public class SaTokenConfigFactory { */ @SuppressWarnings("unchecked") private static T getObjectByClass(String str, Class cs) { - Object value = null; - if (str == null) { - value = null; - } else if (cs.equals(String.class)) { + Object value; + if (cs.equals(String.class)) { value = str; } else if (cs.equals(int.class) || cs.equals(Integer.class)) { - value = new Integer(str); + value = Integer.valueOf(str); } else if (cs.equals(long.class) || cs.equals(Long.class)) { - value = new Long(str); + value = Long.valueOf(str); } else if (cs.equals(short.class) || cs.equals(Short.class)) { - value = new Short(str); + value = Short.valueOf(str); } else if (cs.equals(float.class) || cs.equals(Float.class)) { - value = new Float(str); + value = Float.valueOf(str); } else if (cs.equals(double.class) || cs.equals(Double.class)) { - value = new Double(str); + value = Double.valueOf(str); } else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) { - value = new Boolean(str); + value = Boolean.valueOf(str); } else { throw new RuntimeException("未能将值:" + str + ",转换类型为:" + cs, null); } From ec9f12511c8a4c06836242cab745fb20cb485535 Mon Sep 17 00:00:00 2001 From: xy <3293232930@qq.com> Date: Mon, 24 May 2021 17:24:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E9=80=BB=E8=BE=91,=20=E7=9B=AE=E6=B5=8B?= =?UTF-8?q?=E4=BC=9A=E5=BF=AB=E5=BE=88=E5=A4=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action/SaTokenActionDefaultImpl.java | 12 +++++------- .../java/cn/dev33/satoken/util/SaFoxUtil.java | 17 ----------------- 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/action/SaTokenActionDefaultImpl.java b/sa-token-core/src/main/java/cn/dev33/satoken/action/SaTokenActionDefaultImpl.java index 39350bb1..1aa86328 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/action/SaTokenActionDefaultImpl.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/action/SaTokenActionDefaultImpl.java @@ -4,6 +4,7 @@ import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.List; import java.util.UUID; +import java.util.regex.Pattern; import cn.dev33.satoken.SaManager; import cn.dev33.satoken.annotation.SaCheckLogin; @@ -72,14 +73,11 @@ public class SaTokenActionDefaultImpl implements SaTokenAction { if(list == null || list.size() == 0) { return false; } - // 遍历匹配 - for (String patt : list) { - if(SaFoxUtil.vagueMatch(patt, element)) { - return true; - } + if (list.contains(element)) { + return true; + }else{ + return list.stream().anyMatch(patt-> Pattern.matches(patt.replaceAll("\\*", ".*"), element)); } - // 走出for循环说明没有一个元素可以匹配成功 - return false; } /** diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java index 7569efd8..6cbb1e0e 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java @@ -130,21 +130,4 @@ public class SaFoxUtil { return list2; } - /** - * 字符串模糊匹配 - *

example: - *

user* user-add -- true - *

user* art-add -- false - * @param patt 表达式 - * @param str 待匹配的字符串 - * @return 是否可以匹配 - */ - public static boolean vagueMatch(String patt, String str) { - // 如果表达式不带有*号,则只需简单equals即可 (速度提升200倍) - if(patt.indexOf("*") == -1) { - return patt.equals(str); - } - return Pattern.matches(patt.replaceAll("\\*", ".*"), str); - } - }