refactor(sa-token-core): 重构 SaCheckOrHandler 类

-将 checkMethod 方法中的逻辑抽取到新的 _checkMethod 方法中
- 新增 _annotationList 方法,用于获取注解列表
- 优化代码结构,提高可读性和可维护性
This commit is contained in:
Kairos 2025-06-09 13:56:35 +08:00
parent b0d42821d7
commit 56ed3aa32a

View File

@ -15,7 +15,7 @@
*/
package cn.dev33.satoken.annotation.handler;
import cn.dev33.satoken.annotation.*;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
@ -40,38 +40,15 @@ public class SaCheckOrHandler implements SaAnnotationHandlerInterface<SaCheckOr>
@Override
public void checkMethod(SaCheckOr at, AnnotatedElement element) {
_checkMethod(at.login(), at.role(), at.permission(), at.safe(), at.httpBasic(), at.httpDigest(), at.disable(), at.append(), element);
_checkMethod(at, element);
}
public static void _checkMethod(
SaCheckLogin[] login,
SaCheckRole[] role,
SaCheckPermission[] permission,
SaCheckSafe[] safe,
SaCheckHttpBasic[] httpBasic,
SaCheckHttpDigest[] httpDigest,
SaCheckDisable[] disable,
Class<? extends Annotation>[] append,
AnnotatedElement element
) {
public static void _checkMethod(SaCheckOr at, AnnotatedElement element) {
// 先把所有注解塞到一个 list
List<Annotation> annotationList = new ArrayList<>();
annotationList.addAll(Arrays.asList(login));
annotationList.addAll(Arrays.asList(role));
annotationList.addAll(Arrays.asList(permission));
annotationList.addAll(Arrays.asList(safe));
annotationList.addAll(Arrays.asList(disable));
annotationList.addAll(Arrays.asList(httpBasic));
annotationList.addAll(Arrays.asList(httpDigest));
for (Class<? extends Annotation> annotationClass : append) {
Annotation annotation = SaAnnotationStrategy.instance.getAnnotation.apply(element, annotationClass);
if(annotation != null) {
annotationList.add(annotation);
}
}
List<Annotation> annotationList = _annotationList(at, element);
// 如果 atList 为空说明 SaCheckOr 上不包含任何注解校验我们直接跳过即可
if(annotationList.isEmpty()) {
if (annotationList.isEmpty()) {
return;
}
@ -91,4 +68,26 @@ public class SaCheckOrHandler implements SaAnnotationHandlerInterface<SaCheckOr>
throw errorList.get(0);
}
/**
* 返回注解List供外部调用使用例如增加SaCheckApiKey的或校验
*/
public static List<Annotation> _annotationList(SaCheckOr or, AnnotatedElement element) {
List<Annotation> annotationList = new ArrayList<>();
annotationList.addAll(Arrays.asList(or.login()));
annotationList.addAll(Arrays.asList(or.role()));
annotationList.addAll(Arrays.asList(or.permission()));
annotationList.addAll(Arrays.asList(or.safe()));
annotationList.addAll(Arrays.asList(or.disable()));
annotationList.addAll(Arrays.asList(or.httpBasic()));
annotationList.addAll(Arrays.asList(or.httpDigest()));
for (Class<? extends Annotation> annotationClass : or.append()) {
Annotation annotation = SaAnnotationStrategy.instance.getAnnotation.apply(element, annotationClass);
if (annotation != null) {
annotationList.add(annotation);
}
}
return annotationList;
}
}