注解处理器传递参数改为 Method

This commit is contained in:
click33 2024-08-03 19:22:21 +08:00
parent cd0c20793a
commit c38eb0c68c
18 changed files with 41 additions and 44 deletions

View File

@ -16,7 +16,7 @@
package cn.dev33.satoken.annotation.handler;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 所有注解处理器的父接口
@ -35,18 +35,18 @@ public interface SaAnnotationAbstractHandler<T extends Annotation> {
/**
* 所需要执行的校验方法
* @param at 注解对象
* @param element 被标注的注解的元素引用类或方法
* @param method 被标注的注解的方法引用
*/
@SuppressWarnings("unchecked")
default void check(Annotation at, AnnotatedElement element) {
checkMethod((T) at, element);
default void check(Annotation at, Method method) {
checkMethod((T) at, method);
}
/**
* 所需要执行的校验方法转换类型后
* @param at 注解对象
* @param element 被标注的注解的元素引用类或方法
* @param method 被标注的注解的方法引用
*/
void checkMethod(T at, AnnotatedElement element);
void checkMethod(T at, Method method);
}

View File

@ -19,7 +19,7 @@ import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.SaCheckDisable;
import cn.dev33.satoken.stp.StpLogic;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaCheckDisable 的处理器
@ -35,7 +35,7 @@ public class SaCheckDisableHandler implements SaAnnotationAbstractHandler<SaChec
}
@Override
public void checkMethod(SaCheckDisable at, AnnotatedElement element) {
public void checkMethod(SaCheckDisable at, Method method) {
_checkMethod(at.type(), at.value(), at.level());
}

View File

@ -18,7 +18,7 @@ package cn.dev33.satoken.annotation.handler;
import cn.dev33.satoken.annotation.SaCheckHttpBasic;
import cn.dev33.satoken.httpauth.basic.SaHttpBasicUtil;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaCheckHttpBasic 的处理器
@ -34,7 +34,7 @@ public class SaCheckHttpBasicHandler implements SaAnnotationAbstractHandler<SaCh
}
@Override
public void checkMethod(SaCheckHttpBasic at, AnnotatedElement element) {
public void checkMethod(SaCheckHttpBasic at, Method method) {
_checkMethod(at.realm(), at.account());
}

View File

@ -20,7 +20,7 @@ import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.httpauth.digest.SaHttpDigestUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaCheckHttpDigest 的处理器
@ -36,7 +36,7 @@ public class SaCheckHttpDigestHandler implements SaAnnotationAbstractHandler<SaC
}
@Override
public void checkMethod(SaCheckHttpDigest at, AnnotatedElement element) {
public void checkMethod(SaCheckHttpDigest at, Method method) {
_checkMethod(at.username(), at.password(), at.realm(), at.value());
}

View File

@ -19,7 +19,7 @@ import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpLogic;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaCheckLogin 的处理器
@ -35,7 +35,7 @@ public class SaCheckLoginHandler implements SaAnnotationAbstractHandler<SaCheckL
}
@Override
public void checkMethod(SaCheckLogin at, AnnotatedElement element) {
public void checkMethod(SaCheckLogin at, Method method) {
_checkMethod(at.type());
}

View File

@ -20,7 +20,7 @@ import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -39,8 +39,8 @@ public class SaCheckOrHandler implements SaAnnotationAbstractHandler<SaCheckOr>
}
@Override
public void checkMethod(SaCheckOr at, AnnotatedElement element) {
_checkMethod(at.login(), at.role(), at.permission(), at.safe(), at.httpBasic(), at.httpDigest(), at.disable(), element);
public void checkMethod(SaCheckOr at, Method method) {
_checkMethod(at.login(), at.role(), at.permission(), at.safe(), at.httpBasic(), at.httpDigest(), at.disable(), method);
}
public static void _checkMethod(
@ -51,7 +51,7 @@ public class SaCheckOrHandler implements SaAnnotationAbstractHandler<SaCheckOr>
SaCheckHttpBasic[] httpBasic,
SaCheckHttpDigest[] httpDigest,
SaCheckDisable[] disable,
AnnotatedElement element
Method method
) {
// 先把所有注解塞到一个 list
List<Annotation> annotationList = new ArrayList<>();
@ -72,7 +72,7 @@ public class SaCheckOrHandler implements SaAnnotationAbstractHandler<SaCheckOr>
List<SaTokenException> errorList = new ArrayList<>();
for (Annotation item : annotationList) {
try {
SaAnnotationStrategy.instance.annotationHandlerMap.get(item.annotationType()).check(item, element);
SaAnnotationStrategy.instance.annotationHandlerMap.get(item.annotationType()).check(item, method);
// 只要有一个校验通过就可以直接返回了
return;
} catch (SaTokenException e) {

View File

@ -22,7 +22,7 @@ import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.util.SaFoxUtil;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaCheckPermission 的处理器
@ -38,7 +38,7 @@ public class SaCheckPermissionHandler implements SaAnnotationAbstractHandler<SaC
}
@Override
public void checkMethod(SaCheckPermission at, AnnotatedElement element) {
public void checkMethod(SaCheckPermission at, Method method) {
_checkMethod(at.type(), at.value(), at.mode(), at.orRole());
}

View File

@ -20,7 +20,7 @@ import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpLogic;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaCheckRole 的处理器
@ -36,7 +36,7 @@ public class SaCheckRoleHandler implements SaAnnotationAbstractHandler<SaCheckRo
}
@Override
public void checkMethod(SaCheckRole at, AnnotatedElement element) {
public void checkMethod(SaCheckRole at, Method method) {
_checkMethod(at.type(), at.value(), at.mode());
}

View File

@ -19,7 +19,7 @@ import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.SaCheckSafe;
import cn.dev33.satoken.stp.StpLogic;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaCheckSafe 的处理器
@ -35,7 +35,7 @@ public class SaCheckSafeHandler implements SaAnnotationAbstractHandler<SaCheckSa
}
@Override
public void checkMethod(SaCheckSafe at, AnnotatedElement element) {
public void checkMethod(SaCheckSafe at, Method method) {
_checkMethod(at.type(), at.value());
}

View File

@ -18,7 +18,7 @@ package cn.dev33.satoken.annotation.handler;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.router.SaRouter;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaIgnore 的处理器
@ -34,7 +34,7 @@ public class SaIgnoreHandler implements SaAnnotationAbstractHandler<SaIgnore> {
}
@Override
public void checkMethod(SaIgnore at, AnnotatedElement element) {
public void checkMethod(SaIgnore at, Method method) {
_checkMethod();
}

View File

@ -102,7 +102,7 @@ public final class SaAnnotationStrategy {
// 先校验 Method 所属 Class 上的注解
Annotation classTakeAnnotation = instance.getAnnotation.apply(method.getDeclaringClass(), (Class<Annotation>)entry.getKey());
if(classTakeAnnotation != null) {
entry.getValue().check(classTakeAnnotation, method.getDeclaringClass());
entry.getValue().check(classTakeAnnotation, method);
}
// 再校验 Method 上的注解

View File

@ -1,7 +1,6 @@
package com.pj.cases.test;
import cn.dev33.satoken.util.SaResult;
import com.pj.satoken.custom_annotation.CheckAccount;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -16,7 +15,6 @@ public class TestController {
// 测试 浏览器访问 http://localhost:8081/test/test
@RequestMapping("test")
@CheckAccount(name = "sa", pwd = "123456")
public SaResult test() {
System.out.println("------------进来了");
return SaResult.ok();

View File

@ -6,7 +6,6 @@ import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
import cn.dev33.satoken.strategy.SaStrategy;
import cn.dev33.satoken.util.SaResult;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -118,9 +117,9 @@ public class SaTokenConfigure implements WebMvcConfigurer {
@PostConstruct
public void rewriteSaStrategy() {
// 重写Sa-Token的注解处理器增加注解合并功能
// SaAnnotationStrategy.instance.getAnnotation = (element, annotationClass) -> {
// return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass);
// };
SaAnnotationStrategy.instance.getAnnotation = (element, annotationClass) -> {
return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass);
};
}
}

View File

@ -6,7 +6,7 @@ import cn.dev33.satoken.exception.SaTokenException;
import com.pj.satoken.custom_annotation.CheckAccount;
import org.springframework.stereotype.Component;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 CheckAccount 的处理器
@ -25,7 +25,7 @@ public class CheckAccountHandler implements SaAnnotationAbstractHandler<CheckAcc
// 每次请求校验注解时会执行的方法
@Override
public void checkMethod(CheckAccount at, AnnotatedElement element) {
public void checkMethod(CheckAccount at, Method method) {
// 获取前端请求提交的参数
String name = SaHolder.getRequest().getParamNotNull("name");
String pwd = SaHolder.getRequest().getParamNotNull("pwd");

View File

@ -6,7 +6,7 @@ import com.pj.satoken.StpUserUtil;
import com.pj.satoken.custom_annotation.SaUserCheckLogin;
import org.springframework.stereotype.Component;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaUserCheckLogin 的处理器
@ -22,7 +22,7 @@ public class SaUserCheckLoginHandler implements SaAnnotationAbstractHandler<SaUs
}
@Override
public void checkMethod(SaUserCheckLogin at, AnnotatedElement element) {
public void checkMethod(SaUserCheckLogin at, Method method) {
SaCheckLoginHandler._checkMethod(StpUserUtil.TYPE);
}

View File

@ -6,7 +6,7 @@ import com.pj.satoken.StpUserUtil;
import com.pj.satoken.custom_annotation.SaUserCheckPermission;
import org.springframework.stereotype.Component;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaUserCheckPermission 的处理器
@ -22,7 +22,7 @@ public class SaUserCheckPermissionHandler implements SaAnnotationAbstractHandler
}
@Override
public void checkMethod(SaUserCheckPermission at, AnnotatedElement element) {
public void checkMethod(SaUserCheckPermission at, Method method) {
SaCheckPermissionHandler._checkMethod(StpUserUtil.TYPE, at.value(), at.mode(), at.orRole());
}

View File

@ -6,7 +6,7 @@ import com.pj.satoken.StpUserUtil;
import com.pj.satoken.custom_annotation.SaUserCheckRole;
import org.springframework.stereotype.Component;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaUserCheckRole 的处理器
@ -22,7 +22,7 @@ public class SaUserCheckRoleHandler implements SaAnnotationAbstractHandler<SaUse
}
@Override
public void checkMethod(SaUserCheckRole at, AnnotatedElement element) {
public void checkMethod(SaUserCheckRole at, Method method) {
SaCheckRoleHandler._checkMethod(StpUserUtil.TYPE, at.value(), at.mode());
}

View File

@ -6,7 +6,7 @@ import com.pj.satoken.StpUserUtil;
import com.pj.satoken.custom_annotation.SaUserCheckSafe;
import org.springframework.stereotype.Component;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
/**
* 注解 SaUserCheckPermission 的处理器
@ -22,7 +22,7 @@ public class SaUserCheckSafeHandler implements SaAnnotationAbstractHandler<SaUse
}
@Override
public void checkMethod(SaUserCheckSafe at, AnnotatedElement element) {
public void checkMethod(SaUserCheckSafe at, Method method) {
SaCheckSafeHandler._checkMethod(StpUserUtil.TYPE, at.value());
}