RequestLoggingAspect.java 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. long startTime = System.currentTimeMillis();
  33. // 捕获请求参数
  34. // Map<String, String[]> parameterMap = request.getParameterMap();
  35. // 获取请求参数
  36. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  37. Method method = signature.getMethod();
  38. Map<String, Object> parameterMap = getParameters(joinPoint, method);
  39. String parameters=JSONObject.toJSONString(parameterMap);
  40. // String parameters = parameterMap.entrySet().stream()
  41. // .map(e -> e.getKey() + "=" + String.join(",", e.getValue()))
  42. // .reduce((a, b) -> a + ", " + b).orElse("");
  43. Object result = joinPoint.proceed();
  44. long endTime = System.currentTimeMillis();
  45. long duration = endTime - startTime;
  46. // 创建请求记录对象
  47. McRequestRecord requestRecord = new McRequestRecord();
  48. requestRecord.setRequestUri(request.getRequestURI());
  49. requestRecord.setRequestMethod(request.getMethod());
  50. requestRecord.setRequestBody(parameters);
  51. requestRecord.setResponseBody(JSONObject.toJSONString(result));
  52. requestRecord.setTimestamp(LocalDateTime.now());
  53. requestRecord.setDuration(duration);
  54. requestRecord.setPid(request.getHeader("pid"));
  55. // 保存请求记录
  56. mcRequestRecordMapper.insert(requestRecord);
  57. return result;
  58. }
  59. private Map<String, Object> getParameters(JoinPoint joinPoint, Method method) {
  60. Object[] args = joinPoint.getArgs();
  61. Map<String, Object> params = new HashMap<>();
  62. // 获取方法参数名称
  63. String[] paramNames = parameterNameDiscoverer.getParameterNames(method);
  64. for (int i = 0; i < paramNames.length; i++) {
  65. if (method.isAnnotationPresent(RequestBody.class)) {
  66. // 如果方法上有 @RequestBody 注解,则获取请求体参数
  67. if (i == 0) { // 假设只有一个 @RequestBody 参数
  68. params.put(paramNames[i], args[i]);
  69. }
  70. } else {
  71. // 如果方法上没有 @RequestBody 注解,则获取普通参数
  72. params.put(paramNames[i], args[i]);
  73. }
  74. }
  75. return params;
  76. }
  77. }