WebLogAspect.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.zkh360.core.config;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.aspectj.lang.JoinPoint;
  4. import org.aspectj.lang.ProceedingJoinPoint;
  5. import org.aspectj.lang.annotation.*;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.web.context.request.RequestContextHolder;
  8. import org.springframework.web.context.request.ServletRequestAttributes;
  9. import javax.servlet.http.HttpServletRequest;
  10. import java.util.Arrays;
  11. @Aspect
  12. @Component
  13. @Slf4j
  14. public class WebLogAspect {
  15. @Pointcut("execution(public * com.zkh360.api..controller.*.*(..))")//两个..代表所有子目录,最后括号里的两个..代表所有参数
  16. public void logPointCut() {
  17. }
  18. @Before("logPointCut()")
  19. public void doBefore(JoinPoint joinPoint) throws Throwable {
  20. // 接收到请求,记录请求内容
  21. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  22. HttpServletRequest request = attributes.getRequest();
  23. // 记录下请求内容
  24. log.info("请求地址 : " + request.getRequestURL().toString());
  25. String requestAuthorization = request.getHeader("Authorization");
  26. log.info("请求的Authorization:"+requestAuthorization);
  27. log.info("请求的终端:"+request.getHeader("user-agent"));
  28. log.info("HTTP METHOD : " + request.getMethod());
  29. log.info("IP : " + request.getRemoteAddr());
  30. log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "."
  31. + joinPoint.getSignature().getName());
  32. //登陆接口用户密码加密
  33. String url = request.getRequestURL().toString().substring(request.getRequestURL().toString().length()-10);
  34. if(url != null && url.equals("user/login")){
  35. String s = Arrays.toString(joinPoint.getArgs());
  36. String params = "";
  37. String invoiceId = "";
  38. String username = "";
  39. String[] split = s.split(",");
  40. String[] rightStr = new String[split.length];
  41. String str = "";
  42. for (int i = 0; i < split.length; i++) {
  43. str = split[2];
  44. String[] temp = split[i].split("=");
  45. rightStr[i] = temp[1];
  46. }
  47. String str2 = "";
  48. String[] split1 = str.split("=");
  49. for (int i = 0; i < split1.length; i++) {
  50. str2 = split1[1];
  51. }
  52. for (int i = 0; i < rightStr.length; i++) {
  53. invoiceId = rightStr[0];
  54. username = rightStr[1];
  55. }
  56. final String passWord = MD5Util.md5(str2);
  57. // params = "invoiceId="+invoiceId+",username="+username+",password="+passWord;
  58. params = "invoiceId="+invoiceId+",username="+username;
  59. log.info("参数 : " + params);
  60. }else{
  61. log.info("参数 : " + Arrays.toString(joinPoint.getArgs()));
  62. }
  63. }
  64. @AfterReturning(returning = "ret", pointcut = "logPointCut()")// returning的值和doAfterReturning的参数名一致
  65. public void doAfterReturning(Object ret) throws Throwable {
  66. // 处理完请求,返回内容
  67. log.info("接口返回值 : " + ret);
  68. }
  69. @Around("logPointCut()")
  70. public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  71. long startTime = System.currentTimeMillis();
  72. Object ob = pjp.proceed();// ob 为方法的返回值
  73. log.info("接口总耗时 : " + (System.currentTimeMillis() - startTime));
  74. return ob;
  75. }
  76. }