feat: mock功能
This commit is contained in:
@@ -48,6 +48,7 @@ import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.nl.core.handler.GlobalExceptionUtil;
|
||||
import org.nl.sys.core.enums.SysBuildInEnum;
|
||||
import org.nl.tool.mock.core.config.MockConfigProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -59,6 +60,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.jdbc.support.JdbcUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.nl.auth.core.util.StpClientUtil;
|
||||
@@ -98,15 +100,45 @@ public class GlobalConfigure implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private SaTokenConfig saTokenConfig;
|
||||
@Autowired
|
||||
private MockInterceptor mockInterceptor;
|
||||
|
||||
private static final String COMMON_REPEAT_SUBMIT_CACHE_KEY = "common-repeatSubmit:";
|
||||
|
||||
public static final String[] NO_CHECK_MOCK = {
|
||||
"/",
|
||||
"/test",
|
||||
"/auth/session/**",
|
||||
"/auth/third/page",
|
||||
"/client/user/**",
|
||||
"/sys/**",
|
||||
"/dev/**",
|
||||
"/gen/basic/**",
|
||||
"/gen/config/**",
|
||||
"/mobile/menu/**",
|
||||
"/mobile/module/**",
|
||||
"/api/agv/status",
|
||||
"/api/device/**",
|
||||
"/api/agv/map/**",
|
||||
"/api/localStorage/**",
|
||||
"/api/baseData/point/status",
|
||||
"/api/language/**",
|
||||
"/favicon.ico",
|
||||
"/doc.html",
|
||||
"/webjars/**",
|
||||
"/v3/api-docs/**",
|
||||
"/druid/**",
|
||||
"/mobile/**",
|
||||
"/auth/**"
|
||||
};
|
||||
|
||||
/**
|
||||
* 无需登录的接口地址集合
|
||||
*/
|
||||
public static final String[] NO_LOGIN_PATH_ARR = {
|
||||
/* 主入口 */
|
||||
"/",
|
||||
"/test",
|
||||
/*AGV*/
|
||||
"/api/agv/status",
|
||||
"/api/device/**",
|
||||
@@ -673,4 +705,21 @@ public class GlobalConfigure implements WebMvcConfigurer {
|
||||
public void registerListenerList(List<CommonDataChangeListener> dataChangeListenerList) {
|
||||
CommonDataChangeEventCenter.registerListenerList(dataChangeListenerList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册拦截器
|
||||
* 配置拦截路径和排除路径
|
||||
*
|
||||
* @param registry 拦截器注册器
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// log.info("Registering MockInterceptor");
|
||||
|
||||
registry.addInterceptor(mockInterceptor)
|
||||
.addPathPatterns("/**") // 拦截所有路径
|
||||
.excludePathPatterns(CollectionUtil.newArrayList(NO_CHECK_MOCK));
|
||||
|
||||
// log.info("MockInterceptor registered successfully");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,12 @@ import org.nl.tool.mock.core.handle.MockService;
|
||||
import org.nl.tool.mock.modular.mockconfig.entity.MockConfig;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -30,6 +34,7 @@ public class MockInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final MockService mockService;
|
||||
private final MockConfigProperties properties;
|
||||
private final PathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
/**
|
||||
* 构造函数,注入依赖
|
||||
@@ -55,13 +60,12 @@ public class MockInterceptor implements HandlerInterceptor {
|
||||
public boolean preHandle(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
Object handler) throws Exception {
|
||||
log.info("check config...");
|
||||
// 检查全局Mock开关
|
||||
if (!properties.isEnabled()) {
|
||||
log.debug("Mock functionality is globally disabled, continuing to controller");
|
||||
return true; // Mock功能禁用,继续执行Controller
|
||||
}
|
||||
|
||||
// todo: 检测白名单
|
||||
|
||||
// 提取请求路径和方法
|
||||
String path = request.getRequestURI();
|
||||
|
||||
Reference in New Issue
Block a user