RequestLoggingAspect.java 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.malk.mc.aspect;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.malk.mc.entity.McRequestRecord;
  4. import com.malk.mc.mapper.McRequestRecordMapper;
  5. import org.aspectj.lang.JoinPoint;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.Around;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.aspectj.lang.reflect.MethodSignature;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
  12. import org.springframework.core.ParameterNameDiscoverer;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import org.springframework.web.context.request.RequestContextHolder;
  16. import org.springframework.web.context.request.ServletRequestAttributes;
  17. import javax.servlet.http.HttpServletRequest;
  18. import java.io.BufferedReader;
  19. import java.lang.reflect.Method;
  20. import java.time.LocalDateTime;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. @Aspect
  24. @Component
  25. public class RequestLoggingAspect {
  26. @Autowired
  27. private McRequestRecordMapper mcRequestRecordMapper;
  28. private ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
  29. @Around("execution(* com.malk.mc.controller..*(..))")
  30. public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
  31. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
  32. if(request.getRequestURI().contains("/api/mc/project/")){
  33. return joinPoint.proceed();
  34. }
  35. long startTime = System.currentTimeMillis();
  36. // 捕获请求参数
  37. // Map<String, String[]> parameterMap = request.getParameterMap();
  38. // 获取请求参数
  39. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  40. Method method = signature.getMethod();
  41. Map<String, Object> parameterMap = getParameters(joinPoint, method);
  42. String parameters=JSONObject.toJSONString(parameterMap);
  43. // String parameters = parameterMap.entrySet().stream()
  44. // .map(e -> e.getKey() + "=" + String.join(",", e.getValue()))
  45. // .reduce((a, b) -> a + ", " + b).orElse("");
  46. Object result = joinPoint.proceed();
  47. long endTime = System.currentTimeMillis();
  48. long duration = endTime - startTime;
  49. // 创建请求记录对象
  50. McRequestRecord requestRecord = new McRequestRecord();
  51. requestRecord.setRequestUri(request.getRequestURI());
  52. requestRecord.setRequestMethod(request.getMethod());
  53. requestRecord.setRequestBody(parameters);
  54. requestRecord.setResponseBody(JSONObject.toJSONString(result));
  55. requestRecord.setTimestamp(LocalDateTime.now());
  56. requestRecord.setDuration(duration);
  57. requestRecord.setPid(request.getHeader("pid"));
  58. // 保存请求记录
  59. mcRequestRecordMapper.insert(requestRecord);
  60. return result;
  61. }
  62. private Map<String, Object> getParameters(JoinPoint joinPoint, Method method) {
  63. Object[] args = joinPoint.getArgs();
  64. Map<String, Object> params = new HashMap<>();
  65. // 获取方法参数名称
  66. String[] paramNames = parameterNameDiscoverer.getParameterNames(method);
  67. for (int i = 0; i < paramNames.length; i++) {
  68. if (method.isAnnotationPresent(RequestBody.class)) {
  69. // 如果方法上有 @RequestBody 注解,则获取请求体参数
  70. if (i == 0) { // 假设只有一个 @RequestBody 参数
  71. params.put(paramNames[i], args[i]);
  72. }
  73. } else {
  74. // 如果方法上没有 @RequestBody 注解,则获取普通参数
  75. params.put(paramNames[i], args[i]);
  76. }
  77. }
  78. return params;
  79. }
  80. }