Skip to content

Commit 54ef6c6

Browse files
committed
feat(crud): CrudRequestMapping 注解增加 ignoreAuth、ignorePermission 支持
1 parent 27dc229 commit 54ef6c6

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/annotation/CrudApi.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
/**
2424
* CRUD(增删改查)API
2525
*
26+
* <p>{@link CrudRequestMapping#apis} 优先级高于方法上的 @CrudApi</p>
27+
*
2628
* @author Charles7c
2729
* @since 2.7.5
2830
*/
@@ -35,4 +37,14 @@
3537
* API 类型
3638
*/
3739
Api value() default Api.LIST;
40+
41+
/**
42+
* 是否忽略认证
43+
*/
44+
boolean ignoreAuth() default false;
45+
46+
/**
47+
* 是否忽略权限校验
48+
*/
49+
boolean ignorePermission() default false;
3850
}

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/annotation/CrudRequestMapping.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,26 @@
3636
*/
3737
String value() default "";
3838

39+
/**
40+
* API 列表
41+
* <p>优先级高于 {@link #api}</p>
42+
*/
43+
CrudApi[] apis() default {};
44+
3945
/**
4046
* API 列表
4147
*/
4248
Api[] api() default {Api.PAGE, Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE, Api.EXPORT};
49+
50+
/**
51+
* 是否忽略认证
52+
* <p>全部忽略,优先级低于 {@link CrudApi#ignoreAuth}</p>
53+
*/
54+
boolean ignoreAuth() default false;
55+
56+
/**
57+
* 是否忽略权限校验
58+
* <p>全部忽略,优先级低于 {@link CrudApi#ignorePermission}</p>
59+
*/
60+
boolean ignorePermission() default false;
4361
}

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/aop/CrudApiAnnotationInterceptor.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616

1717
package top.continew.starter.extension.crud.aop;
1818

19+
import cn.hutool.core.util.ArrayUtil;
1920
import org.aopalliance.intercept.MethodInterceptor;
2021
import org.aopalliance.intercept.MethodInvocation;
2122
import org.springframework.aop.support.AopUtils;
2223
import org.springframework.core.BridgeMethodResolver;
2324
import org.springframework.core.annotation.AnnotatedElementUtils;
2425
import org.springframework.util.ClassUtils;
2526
import top.continew.starter.extension.crud.annotation.CrudApi;
27+
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
2628
import top.continew.starter.extension.crud.controller.AbstractCrudController;
2729

2830
import java.lang.reflect.Method;
31+
import java.util.Arrays;
2932
import java.util.Objects;
3033

3134
/**
@@ -44,10 +47,28 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
4447
Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
4548
Method targetMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
4649
// 获取 @CrudApi 注解
47-
CrudApi crudApi = AnnotatedElementUtils.findMergedAnnotation(targetMethod, CrudApi.class);
50+
CrudApi crudApi = this.getCrudApi(targetMethod, targetClass);
4851
// 执行处理
4952
AbstractCrudController crudController = (AbstractCrudController)invocation.getThis();
5053
crudController.preHandle(crudApi, invocation.getArguments(), targetMethod, targetClass);
5154
return invocation.proceed();
5255
}
56+
57+
/**
58+
* 获取 @CrudApi 注解
59+
*
60+
* @param targetMethod 目标方法
61+
* @param targetClass 目标类
62+
* @return @CrudApi 注解
63+
*/
64+
private CrudApi getCrudApi(Method targetMethod, Class<?> targetClass) {
65+
CrudRequestMapping crudRequestMapping = AnnotatedElementUtils
66+
.findMergedAnnotation(targetClass, CrudRequestMapping.class);
67+
CrudApi[] crudApis = crudRequestMapping.apis();
68+
CrudApi crudApi = AnnotatedElementUtils.findMergedAnnotation(targetMethod, CrudApi.class);
69+
if (ArrayUtil.isEmpty(crudApis)) {
70+
return crudApi;
71+
}
72+
return Arrays.stream(crudApis).filter(api -> api.value() == crudApi.value()).findFirst().orElse(null);
73+
}
5374
}

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/autoconfigure/CrudRequestMappingHandlerMapping.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.lang.reflect.AnnotatedElement;
3333
import java.lang.reflect.Method;
34+
import java.util.Arrays;
3435

3536
/**
3637
* CRUD 请求映射器处理器映射器
@@ -51,17 +52,40 @@ protected RequestMappingInfo getMappingForMethod(@NonNull Method method, @NonNul
5152
return requestMappingInfo;
5253
}
5354
CrudRequestMapping crudRequestMapping = handlerType.getDeclaredAnnotation(CrudRequestMapping.class);
54-
// 过滤 API,如果非本类中定义,且 API 列表中不包含,则忽略
55-
Api[] apiArr = crudRequestMapping.api();
5655
CrudApi crudApi = AnnotatedElementUtils.findMergedAnnotation(method, CrudApi.class);
57-
if (method.getDeclaringClass() != handlerType && !ArrayUtil.contains(apiArr, ExceptionUtils
56+
// 过滤 API:如果非本类中定义,且 API 列表中不包含,则忽略
57+
Api[] apis = this.getApis(crudRequestMapping);
58+
if (method.getDeclaringClass() != handlerType && !ArrayUtil.contains(apis, ExceptionUtils
5859
.exToNull(crudApi::value))) {
5960
return null;
6061
}
6162
// 拼接路径(合并了 @RequestMapping 的部分能力)
6263
return this.getMappingForMethodWrapper(method, handlerType, crudRequestMapping);
6364
}
6465

66+
/**
67+
* 获取 API 列表
68+
*
69+
* @param crudRequestMapping CRUD 请求映射
70+
* @return API 列表
71+
*/
72+
private Api[] getApis(CrudRequestMapping crudRequestMapping) {
73+
Api[] apiArr = crudRequestMapping.api();
74+
CrudApi[] crudApiArr = crudRequestMapping.apis();
75+
if (ArrayUtil.isEmpty(crudApiArr)) {
76+
return apiArr;
77+
}
78+
return Arrays.stream(crudApiArr).map(CrudApi::value).toArray(Api[]::new);
79+
}
80+
81+
/**
82+
* 获取请求映射信息
83+
*
84+
* @param method 方法
85+
* @param handlerType 处理程序类型
86+
* @param crudRequestMapping CRUD 请求映射
87+
* @return 请求映射信息
88+
*/
6589
private RequestMappingInfo getMappingForMethodWrapper(@NonNull Method method,
6690
@NonNull Class<?> handlerType,
6791
CrudRequestMapping crudRequestMapping) {
@@ -81,6 +105,12 @@ private RequestMappingInfo getMappingForMethodWrapper(@NonNull Method method,
81105
return info;
82106
}
83107

108+
/**
109+
* 构建请求映射信息
110+
*
111+
* @param element 元素
112+
* @return 请求映射信息
113+
*/
84114
private RequestMappingInfo buildRequestMappingInfo(AnnotatedElement element) {
85115
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
86116
RequestCondition<?> condition = (element instanceof Class<?> clazz

0 commit comments

Comments
 (0)