mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-11-18 08:40:29 +00:00
fix(security): 单体模式不再对外暴露微服务内部接口,网关拦截外部访问
- 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 <noreply@anthropic.com>
This commit is contained in:
parent
996a4ebecc
commit
1ccce9d6aa
@ -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}
|
||||
* <p>
|
||||
* 只在类路径不存在 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);
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
/**
|
||||
* 单体模式下禁止外部调用微服务内部接口
|
||||
* <p>
|
||||
* /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<String> 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;
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
* <p>
|
||||
* 仅供微服务 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
|
||||
|
||||
@ -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模块 对外接口请求类
|
||||
* <p>
|
||||
* 仅供微服务 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
|
||||
|
||||
@ -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/**
|
||||
* <p>
|
||||
* 这些是各服务提供给其它微服务的 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<String> ALLOW_PATHS = Set.of("/online/api/cgreportgetdatapackage");
|
||||
|
||||
@Override
|
||||
public Mono<Void> 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<Void> 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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user