|
|
|
|
@ -41,9 +41,10 @@ import java.util.ArrayList;
|
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Locale; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 统一记录进入 Controller 调用链的接口访问日志。 |
|
|
|
|
* 统一记录进入 Controller 调用链的非 GET 请求及白名单 GET 请求日志。 |
|
|
|
|
* <p> |
|
|
|
|
* 日志切面优先级高于现有参数检查切面,因此参数检查抛出的业务异常也可以被记录; |
|
|
|
|
* 在 Spring MVC 调用 Controller 之前就被鉴权拦截的请求不会进入本切面。 |
|
|
|
|
@ -62,6 +63,19 @@ public class SystemLogAspect {
|
|
|
|
|
private static final String MASK_VALUE = "******"; |
|
|
|
|
private static final String TRUNCATED_MARK = "...[内容已截断]"; |
|
|
|
|
|
|
|
|
|
/** 存在业务数据写入的 GET 接口,后续新增同类接口时需同步维护。 */ |
|
|
|
|
private static final Set<String> LOGGED_GET_REQUEST_URIS = Set.of( |
|
|
|
|
"/common/updateBaseData", |
|
|
|
|
"/common/rights", |
|
|
|
|
"/rights/supervise", |
|
|
|
|
"/common/mailbox", |
|
|
|
|
"/policeUpdateAvatar", |
|
|
|
|
"/depart/updatePoliceSize", |
|
|
|
|
"/score/police", |
|
|
|
|
"/score/depart", |
|
|
|
|
"/score/personal" |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
private final SystemLogService systemLogService; |
|
|
|
|
private final ObjectMapper objectMapper; |
|
|
|
|
private final RedisTemplate<Object, Object> redisTemplate; |
|
|
|
|
@ -74,12 +88,16 @@ public class SystemLogAspect {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 包裹 Controller 调用并在 finally 中保存日志,确保业务异常场景也能记录。 |
|
|
|
|
* 需要记录的请求在 finally 中保存日志,确保业务异常场景也能记录。 |
|
|
|
|
*/ |
|
|
|
|
@Around("controllerPointcut()") |
|
|
|
|
public Object recordSystemLog(ProceedingJoinPoint joinPoint) throws Throwable { |
|
|
|
|
ServletRequestAttributes attributes = getRequestAttributes(); |
|
|
|
|
HttpServletRequest request = attributes == null ? null : attributes.getRequest(); |
|
|
|
|
if (!shouldRecordLog(request)) { |
|
|
|
|
return joinPoint.proceed(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
HttpServletResponse response = attributes == null ? null : attributes.getResponse(); |
|
|
|
|
LocalDateTime logTime = LocalDateTime.now(); |
|
|
|
|
long startTime = System.nanoTime(); |
|
|
|
|
@ -102,6 +120,17 @@ public class SystemLogAspect {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean shouldRecordLog(HttpServletRequest request) { |
|
|
|
|
if (request == null || !"GET".equalsIgnoreCase(request.getMethod())) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
String requestPath = request.getServletPath(); |
|
|
|
|
if (requestPath.endsWith("/")) { |
|
|
|
|
requestPath = requestPath.substring(0, requestPath.length() - 1); |
|
|
|
|
} |
|
|
|
|
return LOGGED_GET_REQUEST_URIS.contains(requestPath); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private ServletRequestAttributes getRequestAttributes() { |
|
|
|
|
if (RequestContextHolder.getRequestAttributes() instanceof ServletRequestAttributes attributes) { |
|
|
|
|
return attributes; |
|
|
|
|
|