PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java

https://gitlab.com/mrsunchangemyselfsun/ruoyi-vue
Java | 218 lines | 154 code | 12 blank | 52 comment | 25 complexity | 7e287224d486e95554552d5416e82a29 MD5 | raw file
  1. package com.ruoyi.framework.aspectj;
  2. import java.util.Collection;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.aspectj.lang.JoinPoint;
  7. import org.aspectj.lang.annotation.AfterReturning;
  8. import org.aspectj.lang.annotation.AfterThrowing;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.validation.BindingResult;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import org.springframework.web.servlet.HandlerMapping;
  16. import com.alibaba.fastjson.JSON;
  17. import com.ruoyi.common.annotation.Log;
  18. import com.ruoyi.common.core.domain.model.LoginUser;
  19. import com.ruoyi.common.enums.BusinessStatus;
  20. import com.ruoyi.common.enums.HttpMethod;
  21. import com.ruoyi.common.utils.ServletUtils;
  22. import com.ruoyi.common.utils.StringUtils;
  23. import com.ruoyi.common.utils.ip.IpUtils;
  24. import com.ruoyi.common.utils.SecurityUtils;
  25. import com.ruoyi.framework.manager.AsyncManager;
  26. import com.ruoyi.framework.manager.factory.AsyncFactory;
  27. import com.ruoyi.system.domain.SysOperLog;
  28. /**
  29. * 操作日志记录处理
  30. *
  31. * @author ruoyi
  32. */
  33. @Aspect
  34. @Component
  35. public class LogAspect
  36. {
  37. private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
  38. /**
  39. * 处理完请求后执行
  40. *
  41. * @param joinPoint 切点
  42. */
  43. @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
  44. public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult)
  45. {
  46. handleLog(joinPoint, controllerLog, null, jsonResult);
  47. }
  48. /**
  49. * 拦截异常操作
  50. *
  51. * @param joinPoint 切点
  52. * @param e 异常
  53. */
  54. @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
  55. public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e)
  56. {
  57. handleLog(joinPoint, controllerLog, e, null);
  58. }
  59. protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult)
  60. {
  61. try
  62. {
  63. // 获取当前的用户
  64. LoginUser loginUser = SecurityUtils.getLoginUser();
  65. // *========数据库日志=========*//
  66. SysOperLog operLog = new SysOperLog();
  67. operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
  68. // 请求的地址
  69. String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
  70. operLog.setOperIp(ip);
  71. operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
  72. if (loginUser != null)
  73. {
  74. operLog.setOperName(loginUser.getUsername());
  75. }
  76. if (e != null)
  77. {
  78. operLog.setStatus(BusinessStatus.FAIL.ordinal());
  79. operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
  80. }
  81. // 设置方法名称
  82. String className = joinPoint.getTarget().getClass().getName();
  83. String methodName = joinPoint.getSignature().getName();
  84. operLog.setMethod(className + "." + methodName + "()");
  85. // 设置请求方式
  86. operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
  87. // 处理设置注解上的参数
  88. getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
  89. // 保存数据库
  90. AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
  91. }
  92. catch (Exception exp)
  93. {
  94. // 记录本地异常日志
  95. log.error("==前置通知异常==");
  96. log.error("异常信息:{}", exp.getMessage());
  97. exp.printStackTrace();
  98. }
  99. }
  100. /**
  101. * 获取注解中对方法的描述信息 用于Controller层注解
  102. *
  103. * @param log 日志
  104. * @param operLog 操作日志
  105. * @throws Exception
  106. */
  107. public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLog operLog, Object jsonResult) throws Exception
  108. {
  109. // 设置action动作
  110. operLog.setBusinessType(log.businessType().ordinal());
  111. // 设置标题
  112. operLog.setTitle(log.title());
  113. // 设置操作人类别
  114. operLog.setOperatorType(log.operatorType().ordinal());
  115. // 是否需要保存request,参数和值
  116. if (log.isSaveRequestData())
  117. {
  118. // 获取参数的信息,传入到数据库中。
  119. setRequestValue(joinPoint, operLog);
  120. }
  121. // 是否需要保存response,参数和值
  122. if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult))
  123. {
  124. operLog.setJsonResult(StringUtils.substring(JSON.toJSONString(jsonResult), 0, 2000));
  125. }
  126. }
  127. /**
  128. * 获取请求的参数,放到log中
  129. *
  130. * @param operLog 操作日志
  131. * @throws Exception 异常
  132. */
  133. private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) throws Exception
  134. {
  135. String requestMethod = operLog.getRequestMethod();
  136. if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod))
  137. {
  138. String params = argsArrayToString(joinPoint.getArgs());
  139. operLog.setOperParam(StringUtils.substring(params, 0, 2000));
  140. }
  141. else
  142. {
  143. Map<?, ?> paramsMap = (Map<?, ?>) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
  144. operLog.setOperParam(StringUtils.substring(paramsMap.toString(), 0, 2000));
  145. }
  146. }
  147. /**
  148. * 参数拼装
  149. */
  150. private String argsArrayToString(Object[] paramsArray)
  151. {
  152. String params = "";
  153. if (paramsArray != null && paramsArray.length > 0)
  154. {
  155. for (Object o : paramsArray)
  156. {
  157. if (StringUtils.isNotNull(o) && !isFilterObject(o))
  158. {
  159. try
  160. {
  161. Object jsonObj = JSON.toJSON(o);
  162. params += jsonObj.toString() + " ";
  163. }
  164. catch (Exception e)
  165. {
  166. }
  167. }
  168. }
  169. }
  170. return params.trim();
  171. }
  172. /**
  173. * 判断是否需要过滤的对象。
  174. *
  175. * @param o 对象信息。
  176. * @return 如果是需要过滤的对象,则返回true;否则返回false。
  177. */
  178. @SuppressWarnings("rawtypes")
  179. public boolean isFilterObject(final Object o)
  180. {
  181. Class<?> clazz = o.getClass();
  182. if (clazz.isArray())
  183. {
  184. return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
  185. }
  186. else if (Collection.class.isAssignableFrom(clazz))
  187. {
  188. Collection collection = (Collection) o;
  189. for (Object value : collection)
  190. {
  191. return value instanceof MultipartFile;
  192. }
  193. }
  194. else if (Map.class.isAssignableFrom(clazz))
  195. {
  196. Map map = (Map) o;
  197. for (Object value : map.entrySet())
  198. {
  199. Map.Entry entry = (Map.Entry) value;
  200. return entry.getValue() instanceof MultipartFile;
  201. }
  202. }
  203. return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
  204. || o instanceof BindingResult;
  205. }
  206. }