OperLogAspect.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.izouma.nineth.aspect;
  2. import com.alibaba.fastjson.JSON;
  3. import com.izouma.nineth.annotations.OperLog;
  4. import com.izouma.nineth.domain.ExceptionLog;
  5. import com.izouma.nineth.domain.OperationLog;
  6. import com.izouma.nineth.domain.User;
  7. import com.izouma.nineth.repo.ExceptionLogRepo;
  8. import com.izouma.nineth.repo.OperationLogRepo;
  9. import com.izouma.nineth.utils.IPUtils;
  10. import com.izouma.nineth.utils.SecurityUtils;
  11. import org.aspectj.lang.JoinPoint;
  12. import org.aspectj.lang.annotation.AfterReturning;
  13. import org.aspectj.lang.annotation.AfterThrowing;
  14. import org.aspectj.lang.annotation.Aspect;
  15. import org.aspectj.lang.annotation.Pointcut;
  16. import org.aspectj.lang.reflect.MethodSignature;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.web.context.request.RequestAttributes;
  20. import org.springframework.web.context.request.RequestContextHolder;
  21. import javax.servlet.http.HttpServletRequest;
  22. import java.lang.reflect.Method;
  23. import java.time.LocalDateTime;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. @Aspect
  27. @Component
  28. public class OperLogAspect {
  29. @Autowired
  30. private OperationLogRepo operationLogRepo;
  31. @Autowired
  32. private ExceptionLogRepo exceptionLogRepo;
  33. /**
  34. * 设置操作日志切入点 记录操作日志 在注解的位置切入代码
  35. */
  36. @Pointcut("@annotation(com.izouma.nineth.annotations.OperLog)")
  37. public void operLogPointCut() {
  38. }
  39. /**
  40. * 设置操作异常切入点记录异常日志 扫描所有controller包下操作
  41. */
  42. @Pointcut("execution(* com.izouma.nineth.web..*.*(..))")
  43. public void operExceptionLogPointCut() {
  44. }
  45. /**
  46. * 正常返回通知,拦截用户操作日志,连接点正常执行完成后执行, 如果连接点抛出异常,则不会执行
  47. *
  48. * @param joinPoint 切入点
  49. * @param keys 返回结果
  50. */
  51. @AfterReturning(value = "operLogPointCut()", returning = "keys")
  52. public void saveOperLog(JoinPoint joinPoint, Object keys) {
  53. // 获取RequestAttributes
  54. RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  55. // 从获取RequestAttributes中获取HttpServletRequest的信息
  56. HttpServletRequest request = (HttpServletRequest) requestAttributes
  57. .resolveReference(RequestAttributes.REFERENCE_REQUEST);
  58. OperationLog operationLog = new OperationLog();
  59. try {
  60. // 从切面织入点处通过反射机制获取织入点处的方法
  61. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  62. // 获取切入点所在的方法
  63. Method method = signature.getMethod();
  64. // 获取操作
  65. OperLog operLog = method.getAnnotation(OperLog.class);
  66. if (operLog != null) {
  67. operationLog.setName(operLog.value()); // 操作模块
  68. operationLog.setType(operLog.type()); // 操作类型
  69. operationLog.setDesc(operLog.desc()); // 操作描述
  70. }
  71. // 获取请求的类名
  72. String className = joinPoint.getTarget().getClass().getName();
  73. // 获取请求的方法名
  74. String methodName = method.getName();
  75. methodName = className + "." + methodName;
  76. operationLog.setReqMethod(methodName);
  77. // 请求的参数
  78. Map<String, String> rtnMap = null;
  79. String params = null;
  80. if (request != null) {
  81. rtnMap = convertMap(request.getParameterMap());
  82. params = JSON.toJSONString(rtnMap);
  83. }
  84. operationLog.setReqParams(params);
  85. operationLog.setResp(JSON.toJSONString(keys));
  86. User user = SecurityUtils.getAuthenticatedUser();
  87. if (user != null) {
  88. operationLog.setUserId(String.valueOf(user.getId()));
  89. operationLog.setUsername(user.getUsername());
  90. }
  91. operationLog.setReqIp(IPUtils.getIpAddr(request));
  92. operationLog.setReqUrl(request != null ? request.getRequestURI() : null);
  93. operationLog.setTime(LocalDateTime.now());
  94. operationLogRepo.save(operationLog);
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. /**
  100. * 异常返回通知,用于拦截异常日志信息 连接点抛出异常后执行
  101. *
  102. * @param joinPoint 切入点
  103. * @param e 异常信息
  104. */
  105. @AfterThrowing(pointcut = "operExceptionLogPointCut()", throwing = "e")
  106. public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
  107. // 获取RequestAttributes
  108. RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  109. // 从获取RequestAttributes中获取HttpServletRequest的信息
  110. HttpServletRequest request = (HttpServletRequest) requestAttributes
  111. .resolveReference(RequestAttributes.REFERENCE_REQUEST);
  112. ExceptionLog exceptionLog = new ExceptionLog();
  113. try {
  114. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  115. Method method = signature.getMethod();
  116. String className = joinPoint.getTarget().getClass().getName();
  117. String methodName = method.getName();
  118. methodName = className + "." + methodName;
  119. exceptionLog.setReqMethod(methodName);
  120. Map<String, String> rtnMap = convertMap(request.getParameterMap());
  121. String params = JSON.toJSONString(rtnMap);
  122. exceptionLog.setReqParams(params);
  123. exceptionLog.setName(e.getClass().getName());
  124. exceptionLog.setMessage(stackTraceToString(e.getClass().getName(), e.getMessage(), e
  125. .getStackTrace()));
  126. User user = SecurityUtils.getAuthenticatedUser();
  127. if (user != null) {
  128. exceptionLog.setUserId(String.valueOf(user.getId()));
  129. exceptionLog.setUsername(user.getUsername());
  130. }
  131. exceptionLog.setReqUrl(request.getRequestURI());
  132. exceptionLog.setReqIp(IPUtils.getIpAddr(request));
  133. exceptionLog.setTime(LocalDateTime.now());
  134. exceptionLogRepo.save(exceptionLog);
  135. } catch (Exception e2) {
  136. e2.printStackTrace();
  137. }
  138. }
  139. /**
  140. * 转换request 请求参数
  141. *
  142. * @param paramMap request获取的参数数组
  143. */
  144. public Map<String, String> convertMap(Map<String, String[]> paramMap) {
  145. Map<String, String> rtnMap = new HashMap<>();
  146. for (String key : paramMap.keySet()) {
  147. rtnMap.put(key, paramMap.get(key)[0]);
  148. }
  149. return rtnMap;
  150. }
  151. /**
  152. * 转换异常信息为字符串
  153. *
  154. * @param exceptionName 异常名称
  155. * @param exceptionMessage 异常信息
  156. * @param elements 堆栈信息
  157. */
  158. public String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {
  159. StringBuilder strBuff = new StringBuilder();
  160. for (StackTraceElement stet : elements) {
  161. strBuff.append(stet).append("\n");
  162. }
  163. return exceptionName + ":" + exceptionMessage + "\n\t" + strBuff;
  164. }
  165. }