mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-11-18 08:40:29 +00:00
fix(security): actuator 端点限 admin 访问,请求追踪记录脱敏
- ShiroConfig:/actuator/** 要求 admin 角色,普通登录用户不能再读取请求追踪
- CustomInMemoryHttpTraceRepository:记录前将 X-Access-Token、Authorization、Cookie、X-Sign 等请求头和 URL 中的 token 参数替换为占位符
- ActuatorMemoryController、ActuatorRedisController:监控接口加 @RequiresRoles("admin")
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c8d3326d5c
commit
98600998c8
@ -180,7 +180,11 @@ public class ShiroConfig {
|
||||
filterChainDefinitionMap.put("/sys/version/app3version", "anon");
|
||||
|
||||
//性能监控——安全隐患泄露TOEKN(durid连接池也有)
|
||||
//filterChainDefinitionMap.put("/actuator/**", "anon");
|
||||
//update-begin---author:scott ---date:20261024 for:actuator端点限admin角色访问,避免普通用户通过请求追踪获取他人token------------
|
||||
filterChainDefinitionMap.put("/actuator/health", "anon");
|
||||
filterChainDefinitionMap.put("/actuator/health/**", "anon");
|
||||
filterChainDefinitionMap.put("/actuator/**", "jwt,roles[admin]");
|
||||
//update-end---author:scott ---date:20261024 for:actuator端点限admin角色访问------------
|
||||
//测试模块排除
|
||||
filterChainDefinitionMap.put("/test/seata/**", "anon");
|
||||
|
||||
|
||||
@ -3,7 +3,13 @@ package org.jeecg.modules.monitor.actuator.httptrace;
|
||||
import org.springframework.boot.actuate.web.exchanges.HttpExchange;
|
||||
import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ -30,7 +36,75 @@ public class CustomInMemoryHttpTraceRepository extends InMemoryHttpExchangeRepos
|
||||
// 只有当请求不是OPTIONS方法,并且URI不包含httptrace时才记录数据
|
||||
if (!"OPTIONS".equals(trace.getRequest().getMethod()) &&
|
||||
!trace.getRequest().getUri().toString().contains("httptrace")) {
|
||||
super.add(trace);
|
||||
//update-begin---author:scott ---date:20261023 for:请求追踪记录前脱敏,避免token、cookie等凭据经追踪接口泄露------------
|
||||
super.add(desensitize(trace));
|
||||
//update-end---author:scott ---date:20261023 for:请求追踪记录前脱敏------------
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 脱敏占位符
|
||||
*/
|
||||
private static final String MASK = "******";
|
||||
|
||||
/**
|
||||
* 需要脱敏的请求头/响应头(小写比较)
|
||||
*/
|
||||
private static final Set<String> SENSITIVE_HEADERS = Set.of(
|
||||
"x-access-token", "authorization", "proxy-authorization", "cookie", "set-cookie", "x-sign");
|
||||
|
||||
/**
|
||||
* URL 上携带凭据的参数,如 ?token=xxx
|
||||
*/
|
||||
private static final Pattern SENSITIVE_QUERY = Pattern.compile("(?i)([?&](?:token|access_token|x-access-token)=)[^&#]*");
|
||||
|
||||
/**
|
||||
* 复制一份请求追踪记录,把请求/响应中的凭据替换为占位符后再存储
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-23 请求追踪记录脱敏,避免泄露 X-Access-Token
|
||||
*/
|
||||
private static HttpExchange desensitize(HttpExchange trace) {
|
||||
HttpExchange.Request request = trace.getRequest();
|
||||
HttpExchange.Request safeRequest = new HttpExchange.Request(maskUri(request.getUri()), request.getRemoteAddress(),
|
||||
request.getMethod(), maskHeaders(request.getHeaders()));
|
||||
HttpExchange.Response response = trace.getResponse();
|
||||
HttpExchange.Response safeResponse = response == null ? null
|
||||
: new HttpExchange.Response(response.getStatus(), maskHeaders(response.getHeaders()));
|
||||
return new HttpExchange(trace.getTimestamp(), safeRequest, safeResponse, trace.getPrincipal(), trace.getSession(), trace.getTimeTaken());
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求头/响应头脱敏:命中 SENSITIVE_HEADERS 的头(不区分大小写)值替换为占位符
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-23 请求追踪记录脱敏,避免泄露 X-Access-Token
|
||||
*/
|
||||
private static Map<String, List<String>> maskHeaders(Map<String, List<String>> headers) {
|
||||
if (headers == null || headers.isEmpty()) {
|
||||
return headers;
|
||||
}
|
||||
Map<String, List<String>> safeHeaders = new LinkedHashMap<>(headers.size());
|
||||
headers.forEach((name, values) -> safeHeaders.put(name,
|
||||
name != null && SENSITIVE_HEADERS.contains(name.toLowerCase(Locale.ROOT)) ? List.of(MASK) : values));
|
||||
return safeHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* URL 脱敏:token/access_token/x-access-token 参数值替换为占位符
|
||||
*
|
||||
* @author scott
|
||||
* @since 2026-10-23 请求追踪记录脱敏,避免泄露 X-Access-Token
|
||||
*/
|
||||
private static URI maskUri(URI uri) {
|
||||
if (uri == null || uri.getRawQuery() == null) {
|
||||
return uri;
|
||||
}
|
||||
try {
|
||||
return URI.create(SENSITIVE_QUERY.matcher(uri.toString()).replaceAll("$1" + MASK));
|
||||
} catch (IllegalArgumentException e) {
|
||||
//替换后无法解析时,宁可丢掉查询参数也不记录原值
|
||||
return URI.create(uri.toString().split("\\?", 2)[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package org.jeecg.modules.monitor.controller;
|
||||
|
||||
import org.apache.shiro.authz.annotation.RequiresRoles;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -27,6 +28,7 @@ public class ActuatorMemoryController {
|
||||
* 内存详情
|
||||
* @return
|
||||
*/
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping("/info")
|
||||
public Result<?> getRedisInfo() {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package org.jeecg.modules.monitor.controller;
|
||||
|
||||
import org.apache.shiro.authz.annotation.RequiresRoles;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
@ -36,6 +37,7 @@ public class ActuatorRedisController {
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping("/info")
|
||||
public Result<?> getRedisInfo() throws Exception {
|
||||
List<RedisInfo> infoList = this.redisService.getRedisInfo();
|
||||
@ -50,12 +52,14 @@ public class ActuatorRedisController {
|
||||
* @author chenrui
|
||||
* @date 2024/5/14 14:56
|
||||
*/
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping(value = "/metrics/history")
|
||||
public Result<?> getMetricsHistory() throws Exception {
|
||||
Map<String,List<Map<String,Object>>> metricsHistory = this.redisService.getMetricsHistory();
|
||||
return Result.OK(metricsHistory);
|
||||
}
|
||||
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping("/keysSize")
|
||||
public Map<String, Object> getKeysSize() throws Exception {
|
||||
return redisService.getKeysSize();
|
||||
@ -66,6 +70,7 @@ public class ActuatorRedisController {
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping("/keysSizeForReport")
|
||||
public Map<String, JSONArray> getKeysSizeReport() throws Exception {
|
||||
return redisService.getMapForReport("1");
|
||||
@ -76,6 +81,7 @@ public class ActuatorRedisController {
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping("/memoryForReport")
|
||||
public Map<String, JSONArray> memoryForReport() throws Exception {
|
||||
return redisService.getMapForReport("2");
|
||||
@ -85,11 +91,13 @@ public class ActuatorRedisController {
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping("/infoForReport")
|
||||
public Map<String, JSONArray> infoForReport() throws Exception {
|
||||
return redisService.getMapForReport("3");
|
||||
}
|
||||
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping("/memoryInfo")
|
||||
public Map<String, Object> getMemoryInfo() throws Exception {
|
||||
return redisService.getMemoryInfo();
|
||||
@ -101,6 +109,7 @@ public class ActuatorRedisController {
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@RequiresRoles("admin")
|
||||
@GetMapping("/queryDiskInfo")
|
||||
public Result<List<Map<String,Object>>> queryDiskInfo(HttpServletRequest request, HttpServletResponse response){
|
||||
Result<List<Map<String,Object>>> res = new Result<>();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user