LogAspect.java 7.8 KB

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