From 1ccce9d6aa393dc297a70dfc08ee831f6b4dd840 Mon Sep 17 00:00:00 2001 From: zhangdaiscott Date: Wed, 14 Oct 2026 09:30:11 -0400 Subject: [PATCH] =?UTF-8?q?fix(security):=20=E5=8D=95=E4=BD=93=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8D=E5=86=8D=E5=AF=B9=E5=A4=96=E6=9A=B4=E9=9C=B2?= =?UTF-8?q?=E5=BE=AE=E6=9C=8D=E5=8A=A1=E5=86=85=E9=83=A8=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E7=BD=91=E5=85=B3=E6=8B=A6=E6=88=AA=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - SystemApiController(/sys/api)、AiragBaseApiController(/airag/api)只在微服务模式(存在 jeecg-boot-starter-cloud)加载 - 新增 InnerApiGuardInterceptor/InnerApiGuardConfiguration:单体模式按匹配到的接口路径模板拦截 /sys/api、/airag/api、/online/api、/drag/api,放行前端字典 /online/api/cgreportGetDataPackage - 网关新增 InnerApiBlockFilter:外部经网关访问上述内部接口返回 403;Feign 按服务名直连不经网关,不受影响 Co-Authored-By: Claude Opus 5.5 --- .../InnerApiGuardConfiguration.java | 25 +++++ .../interceptor/InnerApiGuardInterceptor.java | 67 +++++++++++++ .../controller/AiragBaseApiController.java | 5 + .../api/controller/SystemApiController.java | 5 + .../org/jeecg/filter/InnerApiBlockFilter.java | 93 +++++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/firewall/interceptor/InnerApiGuardConfiguration.java create mode 100644 jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/firewall/interceptor/InnerApiGuardInterceptor.java create mode 100644 jeecg-boot/jeecg-server-cloud/jeecg-cloud-gateway/src/main/java/org/jeecg/filter/InnerApiBlockFilter.java diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/firewall/interceptor/InnerApiGuardConfiguration.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/firewall/interceptor/InnerApiGuardConfiguration.java new file mode 100644 index 000000000..e0fa4bdcf --- /dev/null +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/firewall/interceptor/InnerApiGuardConfiguration.java @@ -0,0 +1,25 @@ +package org.jeecg.config.firewall.interceptor; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * 单体模式注册内部接口拦截器 {@link InnerApiGuardInterceptor} + *

+ * 只在类路径不存在 jeecg-boot-starter-cloud 时(单体模式)生效;微服务模式下内部接口需供 Feign 调用,由网关拦截外部访问。 + * + * @author scott + * @since 2026-10-14 单体模式下内部接口可被任意登录用户直接调用 + */ +@Configuration +@ConditionalOnMissingClass("org.jeecg.config.JeecgCloudBaseConfig") +public class InnerApiGuardConfiguration implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new InnerApiGuardInterceptor()).addPathPatterns("/**").order(Ordered.HIGHEST_PRECEDENCE); + } +} diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/firewall/interceptor/InnerApiGuardInterceptor.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/firewall/interceptor/InnerApiGuardInterceptor.java new file mode 100644 index 000000000..f617daaee --- /dev/null +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/firewall/interceptor/InnerApiGuardInterceptor.java @@ -0,0 +1,67 @@ +package org.jeecg.config.firewall.interceptor; + +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.api.vo.Result; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.HandlerMapping; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Set; + +/** + * 单体模式下禁止外部调用微服务内部接口 + *

+ * /sys/api、/airag/api、/online/api、/drag/api 等是提供给其它微服务 Feign 调用的内部接口, + * 单体模式下各模块为进程内调用,不需要对外暴露。其中部分接口位于依赖 jar 中无法直接修改, + * 因此按 Spring 实际匹配到的接口路径模板统一拦截(不受 URL 编码、大小写、重复斜杠等写法影响)。 + * + * @author scott + * @since 2026-10-14 单体模式下内部接口可被任意登录用户直接调用(读写任意数据、读取数据源口令等) + */ +@Slf4j +public class InnerApiGuardInterceptor implements HandlerInterceptor { + + /** + * 微服务内部接口路径前缀 + */ + private static final String[] INNER_API_PREFIXES = {"/sys/api/", "/airag/api/", "/online/api/", "/drag/api/"}; + + /** + * 前端在用、需要放行的接口(Online 报表字典翻译) + */ + private static final Set ALLOW_PATTERNS = Set.of("/online/api/cgreportGetDataPackage"); + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { + if (!(handler instanceof HandlerMethod)) { + return true; + } + Object pattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); + if (pattern == null) { + return true; + } + String matched = pattern.toString(); + if (isInnerApi(matched) && !ALLOW_PATTERNS.contains(matched)) { + log.warn("单体模式禁止外部调用内部接口: {}", request.getRequestURI()); + response.setStatus(HttpServletResponse.SC_FORBIDDEN); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json; charset=utf-8"); + response.getWriter().print(JSON.toJSONString(Result.error(HttpServletResponse.SC_FORBIDDEN, "内部接口禁止外部访问"))); + return false; + } + return true; + } + + private static boolean isInnerApi(String pattern) { + for (String prefix : INNER_API_PREFIXES) { + if (pattern.startsWith(prefix)) { + return true; + } + } + return false; + } +} diff --git a/jeecg-boot/jeecg-boot-module/jeecg-boot-module-airag/src/main/java/org/jeecg/modules/airag/llm/controller/AiragBaseApiController.java b/jeecg-boot/jeecg-boot-module/jeecg-boot-module-airag/src/main/java/org/jeecg/modules/airag/llm/controller/AiragBaseApiController.java index 1c8627e52..e6fb31768 100644 --- a/jeecg-boot/jeecg-boot-module/jeecg-boot-module-airag/src/main/java/org/jeecg/modules/airag/llm/controller/AiragBaseApiController.java +++ b/jeecg-boot/jeecg-boot-module/jeecg-boot-module-airag/src/main/java/org/jeecg/modules/airag/llm/controller/AiragBaseApiController.java @@ -3,17 +3,22 @@ package org.jeecg.modules.airag.llm.controller; import org.jeecg.common.airag.api.IAiragBaseApi; import org.jeecg.modules.airag.api.AiragBaseApiImpl; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * airag baseAPI Controller + *

+ * 仅供微服务 Feign 内部调用。单体模式下为进程内调用,无需对外暴露, + * 故只在类路径存在 jeecg-boot-starter-cloud(微服务模式)时加载(2026-10-14) * * @author sjlei * @date 2025-12-30 */ @RestController("airagBaseApiController") +@ConditionalOnClass(name = "org.jeecg.config.JeecgCloudBaseConfig") public class AiragBaseApiController implements IAiragBaseApi { @Autowired diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/SystemApiController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/SystemApiController.java index 83ee17e31..0319657db 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/SystemApiController.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/SystemApiController.java @@ -15,6 +15,7 @@ import org.jeecg.common.system.vo.*; import org.jeecg.modules.system.service.ISysUserService; import org.jeecg.modules.system.service.impl.SysBaseApiImpl; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; @@ -26,11 +27,15 @@ import java.util.Set; /** * 服务化 system模块 对外接口请求类 + *

+ * 仅供微服务 Feign 内部调用。单体模式下 ISysBaseAPI 为进程内调用,无需对外暴露, + * 故只在类路径存在 jeecg-boot-starter-cloud(微服务模式)时加载,防止 /sys/api/** 被普通登录用户直接调用(2026-10-14) * @author: jeecg-boot */ @Slf4j @RestController @RequestMapping("/sys/api") +@ConditionalOnClass(name = "org.jeecg.config.JeecgCloudBaseConfig") public class SystemApiController { @Autowired diff --git a/jeecg-boot/jeecg-server-cloud/jeecg-cloud-gateway/src/main/java/org/jeecg/filter/InnerApiBlockFilter.java b/jeecg-boot/jeecg-server-cloud/jeecg-cloud-gateway/src/main/java/org/jeecg/filter/InnerApiBlockFilter.java new file mode 100644 index 000000000..a01dadc3c --- /dev/null +++ b/jeecg-boot/jeecg-server-cloud/jeecg-cloud-gateway/src/main/java/org/jeecg/filter/InnerApiBlockFilter.java @@ -0,0 +1,93 @@ +package org.jeecg.filter; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.util.UriUtils; +import reactor.core.publisher.Mono; + +import java.nio.charset.StandardCharsets; +import java.util.Locale; +import java.util.Set; + +/** + * 禁止外部经网关访问微服务内部接口 /sys/api/**、/airag/api/**、/online/api/**、/drag/api/** + *

+ * 这些是各服务提供给其它微服务的 Feign 内部接口(含数据源明文口令、用户资料、任意表数据读写等), + * Feign 按服务名直连,不经过网关,因此在网关拦截不影响内部调用。 + * + * @author scott + * @since 2026-10-14 微服务模式下外部请求可经网关直调内部接口 + */ +@Slf4j +@Component +public class InnerApiBlockFilter implements GlobalFilter, Ordered { + + /** + * 微服务内部接口路径前缀(小写) + */ + private static final String[] INNER_API_PREFIXES = {"/sys/api/", "/airag/api/", "/online/api/", "/drag/api/"}; + + /** + * 放行的内部接口(规范化后的小写路径):前端字典翻译在用 + */ + private static final Set ALLOW_PATHS = Set.of("/online/api/cgreportgetdatapackage"); + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + String path = normalizePath(exchange.getRequest().getURI().getRawPath()); + for (String prefix : INNER_API_PREFIXES) { + int idx = path.indexOf(prefix); + if (idx >= 0 || path.endsWith(prefix.substring(0, prefix.length() - 1))) { + String innerPath = idx >= 0 ? path.substring(idx) : path; + if (!ALLOW_PATHS.contains(innerPath)) { + log.warn("拦截外部访问内部接口: {}", exchange.getRequest().getURI().getRawPath()); + return forbidden(exchange.getResponse()); + } + } + } + return chain.filter(exchange); + } + + /** + * 规范化路径,防止编码、矩阵参数、重复斜杠、大小写等方式绕过 + */ + private static String normalizePath(String rawPath) { + if (rawPath == null) { + return ""; + } + String path = rawPath; + // 多次解码,防止 %252F 之类的二次编码 + for (int i = 0; i < 3; i++) { + String decoded = UriUtils.decode(path, StandardCharsets.UTF_8); + if (decoded.equals(path)) { + break; + } + path = decoded; + } + // 去掉矩阵参数(Spring MVC 默认忽略 ;xxx),统一斜杠并合并重复斜杠 + path = path.replace('\\', '/').replaceAll(";[^/]*", "").replaceAll("/{2,}", "/"); + return path.toLowerCase(Locale.ROOT); + } + + private static Mono forbidden(ServerHttpResponse response) { + response.setStatusCode(HttpStatus.FORBIDDEN); + response.getHeaders().setContentType(MediaType.APPLICATION_JSON); + byte[] body = "{\"success\":false,\"code\":403,\"message\":\"内部接口禁止外部访问\"}".getBytes(StandardCharsets.UTF_8); + DataBuffer buffer = response.bufferFactory().wrap(body); + return response.writeWith(Mono.just(buffer)); + } + + @Override + public int getOrder() { + // 早于 GlobalAccessTokenFilter(0) 执行 + return -100; + } +}