LogAspect.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.ruoyi.framework.aspectj;
  2. import java.lang.reflect.Method;
  3. import java.util.Map;
  4. import com.ruoyi.common.utils.AddressUtils;
  5. import org.aspectj.lang.JoinPoint;
  6. import org.aspectj.lang.Signature;
  7. import org.aspectj.lang.annotation.AfterReturning;
  8. import org.aspectj.lang.annotation.AfterThrowing;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Pointcut;
  11. import org.aspectj.lang.reflect.MethodSignature;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.scheduling.annotation.Async;
  16. import org.springframework.scheduling.annotation.EnableAsync;
  17. import org.springframework.stereotype.Component;
  18. import com.alibaba.fastjson.JSONObject;
  19. import com.ruoyi.common.constant.UserConstants;
  20. import com.ruoyi.common.utils.ServletUtils;
  21. import com.ruoyi.common.utils.StringUtils;
  22. import com.ruoyi.common.utils.security.ShiroUtils;
  23. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  24. import com.ruoyi.project.monitor.operlog.domain.OperLog;
  25. import com.ruoyi.project.monitor.operlog.service.IOperLogService;
  26. import com.ruoyi.project.system.user.domain.User;
  27. /**
  28. * 操作日志记录处理
  29. *
  30. * @author ruoyi
  31. */
  32. @Aspect
  33. @Component
  34. @EnableAsync
  35. public class LogAspect
  36. {
  37. private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
  38. @Autowired
  39. private IOperLogService operLogService;
  40. // 配置织入点
  41. @Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.Log)")
  42. public void logPointCut()
  43. {
  44. }
  45. /**
  46. * 前置通知 用于拦截操作
  47. *
  48. * @param joinPoint 切点
  49. */
  50. @AfterReturning(pointcut = "logPointCut()")
  51. public void doBefore(JoinPoint joinPoint)
  52. {
  53. handleLog(joinPoint, null);
  54. }
  55. /**
  56. * 拦截异常操作
  57. *
  58. * @param joinPoint
  59. * @param e
  60. */
  61. @AfterThrowing(value = "logPointCut()", throwing = "e")
  62. public void doAfter(JoinPoint joinPoint, Exception e)
  63. {
  64. handleLog(joinPoint, e);
  65. }
  66. @Async
  67. protected void handleLog(final JoinPoint joinPoint, final Exception e)
  68. {
  69. try
  70. {
  71. // 获得注解
  72. Log controllerLog = getAnnotationLog(joinPoint);
  73. if (controllerLog == null)
  74. {
  75. return;
  76. }
  77. // 获取当前的用户
  78. User currentUser = ShiroUtils.getUser();
  79. // *========数据库日志=========*//
  80. OperLog operLog = new OperLog();
  81. operLog.setStatus(UserConstants.NORMAL);
  82. // 请求的地址
  83. String ip = ShiroUtils.getIp();
  84. operLog.setOperIp(ip);
  85. //操作地点
  86. operLog.setOperLocation(AddressUtils.getRealAddressByIP(ip));
  87. operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
  88. if (currentUser != null)
  89. {
  90. operLog.setLoginName(currentUser.getLoginName());
  91. operLog.setDeptName(currentUser.getDept().getDeptName());
  92. }
  93. if (e != null)
  94. {
  95. operLog.setStatus(UserConstants.EXCEPTION);
  96. operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
  97. }
  98. // 设置方法名称
  99. String className = joinPoint.getTarget().getClass().getName();
  100. String methodName = joinPoint.getSignature().getName();
  101. operLog.setMethod(className + "." + methodName + "()");
  102. // 处理设置注解上的参数
  103. getControllerMethodDescription(controllerLog, operLog);
  104. // 保存数据库
  105. operLogService.insertOperlog(operLog);
  106. }
  107. catch (Exception exp)
  108. {
  109. // 记录本地异常日志
  110. log.error("==前置通知异常==");
  111. log.error("异常信息:{}", exp.getMessage());
  112. exp.printStackTrace();
  113. }
  114. }
  115. /**
  116. * 获取注解中对方法的描述信息 用于Controller层注解
  117. *
  118. * @param joinPoint 切点
  119. * @return 方法描述
  120. * @throws Exception
  121. */
  122. public void getControllerMethodDescription(Log log, OperLog operLog) throws Exception
  123. {
  124. // 设置action动作
  125. operLog.setAction(log.action());
  126. // 设置标题
  127. operLog.setTitle(log.title());
  128. // 设置channel
  129. operLog.setChannel(log.channel());
  130. // 是否需要保存request,参数和值
  131. if (log.isSaveRequestData())
  132. {
  133. // 获取参数的信息,传入到数据库中。
  134. setRequestValue(operLog);
  135. }
  136. }
  137. /**
  138. * 获取请求的参数,放到log中
  139. *
  140. * @param operLog
  141. * @param request
  142. */
  143. private void setRequestValue(OperLog operLog)
  144. {
  145. Map<String, String[]> map = ServletUtils.getRequest().getParameterMap();
  146. String params = JSONObject.toJSONString(map);
  147. operLog.setOperParam(StringUtils.substring(params, 0, 255));
  148. }
  149. /**
  150. * 是否存在注解,如果存在就获取
  151. */
  152. private Log getAnnotationLog(JoinPoint joinPoint) throws Exception
  153. {
  154. Signature signature = joinPoint.getSignature();
  155. MethodSignature methodSignature = (MethodSignature) signature;
  156. Method method = methodSignature.getMethod();
  157. if (method != null)
  158. {
  159. return method.getAnnotation(Log.class);
  160. }
  161. return null;
  162. }
  163. }