LogRequestParam.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package me.zhengjie.aspect;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.serializer.SerializerFeature;
  4. import lombok.extern.slf4j.Slf4j;
  5. import me.zhengjie.utils.IpAddressUtil;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.aspectj.lang.JoinPoint;
  8. import org.aspectj.lang.Signature;
  9. import org.aspectj.lang.annotation.AfterReturning;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Before;
  12. import org.aspectj.lang.annotation.Pointcut;
  13. import org.aspectj.lang.reflect.CodeSignature;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.web.context.request.RequestContextHolder;
  16. import org.springframework.web.context.request.ServletRequestAttributes;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.util.Objects;
  21. @Slf4j
  22. @Aspect
  23. @Component
  24. public class LogRequestParam {
  25. @Pointcut("execution(* me.zhengjie.application.*.controller..*.*(..))")
  26. public void pointCut() {
  27. }
  28. public static boolean isJSON(String str) {
  29. boolean result = false;
  30. if (StringUtils.isNotBlank(str)) {
  31. str = str.trim();
  32. if (str.startsWith("{") && str.endsWith("}")) {
  33. result = true;
  34. } else if (str.startsWith("[") && str.endsWith("]")) {
  35. result = true;
  36. }
  37. }
  38. return result;
  39. }
  40. @Before(value = "pointCut()")
  41. public void doBefore(JoinPoint joinPoint) {
  42. ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
  43. .getRequestAttributes();
  44. if (Objects.isNull(servletRequestAttributes)) {
  45. return;
  46. }
  47. HttpServletRequest request = servletRequestAttributes.getRequest();
  48. // 得到请求的ip地址
  49. String ip = IpAddressUtil.getIpAddress(request);
  50. String uri = request.getRequestURI();
  51. // 请求方法
  52. String method = request.getMethod();
  53. Object error = "";
  54. try {
  55. Object[] parameterValues = joinPoint.getArgs();
  56. int parameterValuesLength = parameterValues.length;
  57. if (parameterValuesLength == 0) {
  58. log.debug("[请求参数] - 无");
  59. return;
  60. }
  61. Signature signature = joinPoint.getSignature();
  62. if (Objects.isNull(signature)) {
  63. return;
  64. }
  65. String[] parameterNames = ((CodeSignature) signature).getParameterNames();
  66. if (Objects.isNull(parameterNames) || parameterNames.length != parameterValuesLength) {
  67. return;
  68. }
  69. String paramJson = "";
  70. for (int i = 0; i < parameterNames.length; i++) {
  71. Object value = parameterValues[i];
  72. if (value == null || value instanceof HttpServletRequest || value instanceof HttpServletResponse) {
  73. continue;
  74. } else if (value instanceof MultipartFile) {
  75. MultipartFile part = (MultipartFile) value;
  76. paramJson = part.getOriginalFilename();
  77. } else if (value instanceof MultipartFile[]) {
  78. MultipartFile[] part = (MultipartFile[]) value;
  79. StringBuffer sb = new StringBuffer();
  80. for (MultipartFile file : part) {
  81. sb.append(file.getOriginalFilename() + ",");
  82. }
  83. paramJson = sb.toString();
  84. } else if (value instanceof String) {
  85. paramJson = (String) value;
  86. if (paramJson.length() > 1000) {
  87. // 去掉特别长的参数
  88. paramJson = paramJson.substring(0, 1000);
  89. }
  90. } else {
  91. // 处理参数是对象的值,如果出现异常,说明有文件流
  92. error = value;
  93. paramJson = JSON.toJSONString(value);
  94. // 先进行赋值
  95. // value = object;
  96. if (StringUtils.isNotBlank(paramJson) && paramJson.length() > 1000) {
  97. StringBuilder sb = new StringBuilder();
  98. // valLength = fieldValue.substring(0, 1000);
  99. sb.append(paramJson.substring(0, 1000));
  100. sb.append(paramJson.substring(paramJson.length() - 1000));
  101. paramJson = sb.toString();
  102. }
  103. }
  104. }
  105. // 这里判断
  106. Object[] obj = { ip, method, uri, paramJson };
  107. log.info("[request] - ip: {} , method: {} , requestUrl: {} , param: {} ", obj);
  108. } catch (Exception e) {
  109. // e.printStackTrace();
  110. // 这里判断
  111. Object[] obj = { ip, method, uri, error.toString() };
  112. log.info("[request] - ip: {} , method: {} , requestUrl: {} , param: {}, ", obj);
  113. }
  114. }
  115. @AfterReturning(returning = "result", pointcut = "pointCut() ")
  116. public void doAfterReturning(Object result) {
  117. try {
  118. ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
  119. .getRequestAttributes();
  120. if (Objects.isNull(servletRequestAttributes)) {
  121. return;
  122. }
  123. HttpServletResponse response = servletRequestAttributes.getResponse();
  124. if (Objects.isNull(response)) {
  125. return;
  126. }
  127. int status = response.getStatus();
  128. HttpServletRequest request = servletRequestAttributes.getRequest();
  129. String json = JSON.toJSONString(result, SerializerFeature.WriteMapNullValue);
  130. // 打印输出的值
  131. if (StringUtils.isNotBlank(json) && json.length() < 1500) {
  132. String uri = request.getRequestURI();
  133. String method = request.getMethod();
  134. Object obj[] = { status, method, uri, json };
  135. log.info("[reponse] - status: {} ,method:{}, url:{} ,result:{}", obj);
  136. }
  137. } catch (Exception e) {
  138. e.printStackTrace();
  139. }
  140. }
  141. }