mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 04:59:21 +08:00
refactor(extension/crud): 增强 BaseController 内 API 校验,支持指定 Controller 单独处理
This commit is contained in:
@@ -23,7 +23,9 @@ import org.springframework.core.BridgeMethodResolver;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.controller.BaseController;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiHandler;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiStrategy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
@@ -36,12 +38,6 @@ import java.util.Objects;
|
||||
*/
|
||||
public class CrudApiAnnotationInterceptor implements MethodInterceptor {
|
||||
|
||||
private final CrudApiHandler handler;
|
||||
|
||||
public CrudApiAnnotationInterceptor(CrudApiHandler handler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
// 获取目标类
|
||||
@@ -51,7 +47,13 @@ public class CrudApiAnnotationInterceptor implements MethodInterceptor {
|
||||
Method targetMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
|
||||
// 获取 @CrudApi 注解
|
||||
CrudApi crudApi = AnnotatedElementUtils.findMergedAnnotation(targetMethod, CrudApi.class);
|
||||
handler.preHandle(crudApi, targetMethod, targetClass);
|
||||
// 获取处理器
|
||||
CrudApiHandler<?> crudApiHandler = CrudApiStrategy.INSTANCE.handlerMap.get(targetClass);
|
||||
if (crudApiHandler != null) {
|
||||
crudApiHandler.preHandle(crudApi, targetMethod, targetClass);
|
||||
} else {
|
||||
CrudApiStrategy.INSTANCE.handlerMap.get(BaseController.class).preHandle(crudApi, targetMethod, targetClass);
|
||||
}
|
||||
return invocation.proceed();
|
||||
}
|
||||
}
|
||||
|
@@ -53,7 +53,7 @@ public class CrudRequestMappingHandlerMapping extends RequestMappingHandlerMappi
|
||||
// 过滤 API,如果非本类中定义,且 API 列表中不包含,则忽略
|
||||
Api[] apiArr = crudRequestMapping.api();
|
||||
Api api = ExceptionUtils.exToNull(() -> Api.valueOf(method.getName().toUpperCase()));
|
||||
if (method.getDeclaringClass() != handlerType && !ArrayUtil.containsAny(apiArr, Api.ALL, api)) {
|
||||
if (method.getDeclaringClass() != handlerType && !ArrayUtil.contains(apiArr, api)) {
|
||||
return null;
|
||||
}
|
||||
// 拼接路径(合并了 @RequestMapping 的部分能力)
|
||||
|
@@ -19,6 +19,7 @@ package top.continew.starter.extension.crud.autoconfigure;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@@ -27,7 +28,9 @@ import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.aop.CrudApiAnnotationAdvisor;
|
||||
import top.continew.starter.extension.crud.aop.CrudApiAnnotationInterceptor;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiHandler;
|
||||
import top.continew.starter.extension.crud.handler.DefaultCrudApiHandler;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiStrategy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* CRUD REST Controller 自动配置
|
||||
@@ -41,6 +44,18 @@ public class CrudRestControllerAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CrudRestControllerAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* 注入自定义处理器
|
||||
*
|
||||
* @param handlerList 自定义处理器集合
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setCrudApiHandler(List<CrudApiHandler<?>> handlerList) {
|
||||
for (CrudApiHandler<?> handler : handlerList) {
|
||||
CrudApiStrategy.INSTANCE.registerHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CRUD API 注解通知
|
||||
*/
|
||||
@@ -55,17 +70,8 @@ public class CrudRestControllerAutoConfiguration {
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CrudApiAnnotationInterceptor crudApiAnnotationInterceptor(CrudApiHandler crudApiHandler) {
|
||||
return new CrudApiAnnotationInterceptor(crudApiHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* CRUD API 处理器(默认)
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CrudApiHandler crudApiHandler() {
|
||||
return new DefaultCrudApiHandler();
|
||||
public CrudApiAnnotationInterceptor crudApiAnnotationInterceptor() {
|
||||
return new CrudApiAnnotationInterceptor();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
|
@@ -24,11 +24,6 @@ package top.continew.starter.extension.crud.enums;
|
||||
*/
|
||||
public enum Api {
|
||||
|
||||
/**
|
||||
* 所有 API
|
||||
*/
|
||||
ALL,
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*/
|
||||
|
@@ -17,6 +17,7 @@
|
||||
package top.continew.starter.extension.crud.handler;
|
||||
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.controller.BaseController;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -26,7 +27,14 @@ import java.lang.reflect.Method;
|
||||
* @author Charles7c
|
||||
* @since 2.7.5
|
||||
*/
|
||||
public interface CrudApiHandler {
|
||||
public interface CrudApiHandler<T extends BaseController> {
|
||||
|
||||
/**
|
||||
* 获取处理器控制器类
|
||||
*
|
||||
* @return 处理器控制器类
|
||||
*/
|
||||
Class<T> getHandlerControllerClass();
|
||||
|
||||
/**
|
||||
* 前置处理
|
||||
|
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.crud.handler;
|
||||
|
||||
import top.continew.starter.extension.crud.controller.BaseController;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* CRUD API 策略
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.5
|
||||
*/
|
||||
public final class CrudApiStrategy {
|
||||
|
||||
/**
|
||||
* 全局单例引用
|
||||
*/
|
||||
public static final CrudApiStrategy INSTANCE = new CrudApiStrategy();
|
||||
|
||||
/**
|
||||
* 处理器集合
|
||||
*/
|
||||
public Map<Class<?>, CrudApiHandler<?>> handlerMap = new LinkedHashMap<>();
|
||||
|
||||
private CrudApiStrategy() {
|
||||
registerDefaultHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册所有默认的处理器
|
||||
*/
|
||||
public void registerDefaultHandler() {
|
||||
handlerMap.put(BaseController.class, new DefaultCrudApiHandler());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册一个处理器
|
||||
*
|
||||
* @param handler 处理器
|
||||
*/
|
||||
public void registerHandler(CrudApiHandler<?> handler) {
|
||||
handlerMap.put(handler.getHandlerControllerClass(), handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除一个注解处理器
|
||||
*
|
||||
* @param controllerClass 控制器类
|
||||
*/
|
||||
public void removeAnnotationHandler(Class<?> controllerClass) {
|
||||
handlerMap.remove(controllerClass);
|
||||
}
|
||||
}
|
@@ -17,6 +17,7 @@
|
||||
package top.continew.starter.extension.crud.handler;
|
||||
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.controller.BaseController;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -26,7 +27,12 @@ import java.lang.reflect.Method;
|
||||
* @author Charles7c
|
||||
* @since 2.7.5
|
||||
*/
|
||||
public class DefaultCrudApiHandler implements CrudApiHandler {
|
||||
public class DefaultCrudApiHandler implements CrudApiHandler<BaseController> {
|
||||
|
||||
@Override
|
||||
public Class getHandlerControllerClass() {
|
||||
return BaseController.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preHandle(CrudApi crudApi, Method targetMethod, Class<?> targetClass) {
|
||||
|
Reference in New Issue
Block a user