feat: mock功能

This commit is contained in:
2026-01-29 14:42:43 +08:00
parent ff6f8aa303
commit 21064a2734
5 changed files with 72 additions and 2 deletions

View File

@@ -17,6 +17,11 @@
<groupId>org.nl.tool.api</groupId>
<artifactId>nl-plugin-tool-api</artifactId>
</dependency>
<dependency>
<groupId>org.nl</groupId>
<artifactId>nl-plugin-auth-api</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -4,6 +4,9 @@ import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* Mock配置属性类
* 使用@ConfigurationProperties从application.yml中绑定mock配置
@@ -13,6 +16,9 @@ import org.springframework.stereotype.Component;
* enabled: true
* cache-expiration-seconds: 300
* log-mock-usage: true
* whitelist:
* - /api/login
* - /api/mes/**
* @author: lyd
* @date: 2026/1/28
*/

View File

@@ -13,6 +13,7 @@
package org.nl.tool.mock.modular.mockconfig.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
@@ -120,4 +121,9 @@ public class MockConfigController {
public CommonResult<MockConfig> detail(@Valid MockConfigIdParam mockConfigIdParam) {
return CommonResult.data(mockConfigService.detail(mockConfigIdParam));
}
@GetMapping("/test")
public String test() {
return "WELCOME";
}
}

View File

@@ -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");
}
}

View File

@@ -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();