Merge pull request #86 from ly-chn/dev

简化部分逻辑
This commit is contained in:
click33 2021-05-24 22:06:02 +08:00 committed by GitHub
commit 34a62a2aa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 67 deletions

View File

@ -1,8 +1,10 @@
package cn.dev33.satoken.action; package cn.dev33.satoken.action;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Pattern;
import cn.dev33.satoken.SaManager; import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.SaCheckLogin; import cn.dev33.satoken.annotation.SaCheckLogin;
@ -71,15 +73,12 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
if(list == null || list.size() == 0) { if(list == null || list.size() == 0) {
return false; return false;
} }
// 遍历匹配 if (list.contains(element)) {
for (String patt : list) {
if(SaFoxUtil.vagueMatch(patt, element)) {
return true; return true;
}else{
return list.stream().anyMatch(patt-> Pattern.matches(patt.replaceAll("\\*", ".*"), element));
} }
} }
// 走出for循环说明没有一个元素可以匹配成功
return false;
}
/** /**
* 对一个Method对象进行注解权限校验注解鉴权逻辑内部实现 * 对一个Method对象进行注解权限校验注解鉴权逻辑内部实现
@ -90,43 +89,28 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
// 获取这个 Method 所属的 Class // 获取这个 Method 所属的 Class
Class<?> clazz = method.getDeclaringClass(); Class<?> clazz = method.getDeclaringClass();
validateAnnotation(clazz);
validateAnnotation(method);
}
// Class 校验 @SaCheckLogin 注解 private void validateAnnotation(AnnotatedElement target) {
if(clazz.isAnnotationPresent(SaCheckLogin.class)) { // 校验 @SaCheckLogin 注解
SaCheckLogin at = clazz.getAnnotation(SaCheckLogin.class); if(target.isAnnotationPresent(SaCheckLogin.class)) {
SaCheckLogin at = target.getAnnotation(SaCheckLogin.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at); SaManager.getStpLogic(at.key()).checkByAnnotation(at);
} }
// Class 校验 @SaCheckRole 注解 // 校验 @SaCheckRole 注解
if(clazz.isAnnotationPresent(SaCheckRole.class)) { if(target.isAnnotationPresent(SaCheckRole.class)) {
SaCheckRole at = clazz.getAnnotation(SaCheckRole.class); SaCheckRole at = target.getAnnotation(SaCheckRole.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at); SaManager.getStpLogic(at.key()).checkByAnnotation(at);
} }
// Class 校验 @SaCheckPermission 注解 // 校验 @SaCheckPermission 注解
if(clazz.isAnnotationPresent(SaCheckPermission.class)) { if(target.isAnnotationPresent(SaCheckPermission.class)) {
SaCheckPermission at = clazz.getAnnotation(SaCheckPermission.class); SaCheckPermission at = target.getAnnotation(SaCheckPermission.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at); 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);
}
} }
} }

View File

@ -94,9 +94,7 @@ public class SaTokenConfigFactory {
Object valueConvert = getObjectByClass(value, field.getType()); Object valueConvert = getObjectByClass(value, field.getType());
field.setAccessible(true); field.setAccessible(true);
field.set(obj, valueConvert); field.set(obj, valueConvert);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("属性赋值出错:" + field.getName(), e);
} catch (IllegalAccessException e) {
throw new RuntimeException("属性赋值出错:" + field.getName(), e); throw new RuntimeException("属性赋值出错:" + field.getName(), e);
} }
} }
@ -112,23 +110,21 @@ public class SaTokenConfigFactory {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static <T> T getObjectByClass(String str, Class<T> cs) { private static <T> T getObjectByClass(String str, Class<T> cs) {
Object value = null; Object value;
if (str == null) { if (cs.equals(String.class)) {
value = null;
} else if (cs.equals(String.class)) {
value = str; value = str;
} else if (cs.equals(int.class) || cs.equals(Integer.class)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) {
value = new Boolean(str); value = Boolean.valueOf(str);
} else { } else {
throw new RuntimeException("未能将值:" + str + ",转换类型为:" + cs, null); throw new RuntimeException("未能将值:" + str + ",转换类型为:" + cs, null);
} }

View File

@ -130,21 +130,4 @@ public class SaFoxUtil {
return list2; return list2;
} }
/**
* 字符串模糊匹配
* <p>example:
* <p> user* user-add -- true
* <p> 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);
}
} }