From d51a18af8de3316a4fae5427ef726f87f22b3b39 Mon Sep 17 00:00:00 2001 From: "zangch@mesnac.com" Date: Fri, 10 Oct 2025 09:33:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(validation):=20wjy=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E8=BF=87=E6=9C=9F=E6=A0=A1=E9=AA=8C=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 ValidationInterceptor 拦截器实现系统过期检查 - 配置拦截器注册,排除 /error 路径 - 实现基于 Base64期日期配置 编码的过读取 - 添加系统过期时的 JSON 格式错误响应 - 提供静态方法供外部调用检查系统状态 - 记录系统过期检查相关日志信息 --- .../validation/InterceptorConfig.java | 28 ++++++ .../validation/ValidationInterceptor.java | 93 +++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 aucma-framework/src/main/java/com/aucma/framework/validation/InterceptorConfig.java create mode 100644 aucma-framework/src/main/java/com/aucma/framework/validation/ValidationInterceptor.java diff --git a/aucma-framework/src/main/java/com/aucma/framework/validation/InterceptorConfig.java b/aucma-framework/src/main/java/com/aucma/framework/validation/InterceptorConfig.java new file mode 100644 index 0000000..81f9d54 --- /dev/null +++ b/aucma-framework/src/main/java/com/aucma/framework/validation/InterceptorConfig.java @@ -0,0 +1,28 @@ +package com.aucma.framework.validation; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * @author Wen JY + * @description: 系统校验拦截器配置 + * @date 2025-10-09 17:14:00 + * @version: 1.0 + */ +@Configuration +public class InterceptorConfig implements WebMvcConfigurer { + + @Bean + public ValidationInterceptor interceptor() { + return new ValidationInterceptor(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(interceptor()) + .addPathPatterns("/**") + .excludePathPatterns("/error"); + } +} diff --git a/aucma-framework/src/main/java/com/aucma/framework/validation/ValidationInterceptor.java b/aucma-framework/src/main/java/com/aucma/framework/validation/ValidationInterceptor.java new file mode 100644 index 0000000..fbc4055 --- /dev/null +++ b/aucma-framework/src/main/java/com/aucma/framework/validation/ValidationInterceptor.java @@ -0,0 +1,93 @@ +package com.aucma.framework.validation; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Base64; + +/** + * @author Wen JY + * @description: 系统校验拦截器 + * @date 2025-10-09 17:20:25 + * @version: 1.0 + */ +public class ValidationInterceptor implements HandlerInterceptor { + + private static final Logger logger = LoggerFactory.getLogger(ValidationInterceptor.class); + + // 配置系统过期日期(可以从配置文件中读取) + @Value("${system.expire.date:MjAyNi0wMS0wMQ==}") // 默认2026-01-01 + private String expireDateBase64; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + // 检查是否已过期 + if (isSystemExpired()) { + handleExpiredRequest(request, response); + return false; // 拦截请求 + } + return true; // 放行请求 + } + + /** + * 检查系统是否已过期 + */ + public boolean isSystemExpired() { + try { + byte[] decode = Base64.getDecoder().decode(expireDateBase64); + String dateStr = new String(decode, StandardCharsets.UTF_8); + + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDate expireDate = LocalDate.parse(dateStr, dtf); + LocalDate currentDate = LocalDate.now(); + + logger.debug("系统过期检查: 当前日期={}, 过期日期={}, 是否过期={}", + currentDate, expireDate, currentDate.isAfter(expireDate)); + + return currentDate.isAfter(expireDate); + + } catch (Exception e) { + logger.error("系统过期检查失败,默认不拦截", e); + return false; // 解析失败时默认不拦截,避免影响系统正常运行 + } + } + + /** + * 处理过期请求 + */ + private void handleExpiredRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { + logger.warn("拦截过期系统访问: {} {}", request.getMethod(), request.getRequestURI()); + + response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); + response.setContentType("application/json;charset=UTF-8"); + + String errorResponse = "{\"code\": 503, \"message\": \"系统已过期,请联系管理员\", \"timestamp\": \"" + + Instant.now().toString() + "\"}"; + + response.getWriter().write(errorResponse); + response.getWriter().flush(); + } + + /** + * 静态方法,供其他类调用检查系统状态 + */ + public static boolean checkSystemStatus() { + try { + ValidationInterceptor interceptor = new ValidationInterceptor(); + interceptor.expireDateBase64 = "MjAyNi0wMS0wMQ=="; //默认 2026-01-01 + return !interceptor.isSystemExpired(); + } catch (Exception e) { + return true; // 检查失败时默认系统可用 + } + } + +}