AutoLogAspect.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.usky.aspect;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.alibaba.fastjson.serializer.PropertyFilter;
  4. import com.usky.annotion.AutoLog;
  5. import com.usky.constant.CommonConstant;
  6. import com.usky.entity.sys.SysLogDTO;
  7. import com.usky.service.log.LogService;
  8. import com.usky.utils.IPUtils;
  9. import com.usky.utils.SpringContextUtils;
  10. import org.aspectj.lang.JoinPoint;
  11. import org.aspectj.lang.ProceedingJoinPoint;
  12. import org.aspectj.lang.annotation.Around;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.aspectj.lang.annotation.Pointcut;
  15. import org.aspectj.lang.reflect.MethodSignature;
  16. import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
  17. import org.springframework.stereotype.Component;
  18. import org.springframework.validation.BindingResult;
  19. import org.springframework.web.multipart.MultipartFile;
  20. import javax.annotation.Resource;
  21. import javax.servlet.ServletRequest;
  22. import javax.servlet.ServletResponse;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.lang.reflect.Method;
  25. import java.sql.Timestamp;
  26. /**
  27. * @author laowo
  28. * @version v1.0
  29. * @date 2020/11/10 14:41
  30. * @description 系统日志切面类
  31. **/
  32. @Aspect
  33. @Component
  34. public class AutoLogAspect {
  35. @Resource
  36. private LogService logService;
  37. @Pointcut("@annotation(com.usky.annotion.AutoLog)")
  38. public void logPointCut() {
  39. }
  40. @Around("logPointCut()")
  41. public Object around(ProceedingJoinPoint point) throws Throwable {
  42. long beginTime = System.currentTimeMillis();
  43. //执行方法
  44. Object result = point.proceed();
  45. //执行时长(毫秒)
  46. long time = System.currentTimeMillis() - beginTime;
  47. //保存日志
  48. saveSysLog(point, time, result);
  49. return result;
  50. }
  51. private void saveSysLog(ProceedingJoinPoint joinPoint, long time, Object obj) {
  52. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  53. Method method = signature.getMethod();
  54. SysLogDTO dto = new SysLogDTO();
  55. AutoLog syslog = method.getAnnotation(AutoLog.class);
  56. if(syslog != null){
  57. //update-begin-author:taoyan date:
  58. String content = syslog.value();
  59. //注解上的描述,操作日志内容
  60. dto.setLogType(syslog.logType());
  61. dto.setLogContent(content);
  62. }
  63. //请求的方法名
  64. String className = joinPoint.getTarget().getClass().getName();
  65. String methodName = signature.getName();
  66. dto.setMethod(className + "." + methodName + "()");
  67. //设置操作类型
  68. if (dto.getLogType() == CommonConstant.LOG_TYPE_2) {
  69. dto.setOperateType(getOperateType(methodName, syslog.operateType()));
  70. }
  71. //获取request
  72. HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
  73. //请求的参数
  74. dto.setRequestType(request.getMethod());
  75. dto.setRequestParam(getReqestParams(request,joinPoint));
  76. //设置IP地址
  77. dto.setIp(IPUtils.getIpAddr(request));
  78. //获取登录用户信息 TODO 添加用户数据后启用
  79. // LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
  80. // if(sysUser!=null){
  81. // dto.setUserid(sysUser.getUsername());
  82. // dto.setUsername(sysUser.getRealname());
  83. // }
  84. //耗时
  85. dto.setCostTime(time);
  86. dto.setCreateTime(new Timestamp(System.currentTimeMillis()));
  87. //保存系统日志
  88. logService.addLog(dto);
  89. }
  90. /**
  91. * 获取操作类型
  92. */
  93. private int getOperateType(String methodName,int operateType) {
  94. if (operateType > 0) {
  95. return operateType;
  96. }
  97. if (methodName.startsWith("list")) {
  98. return CommonConstant.OPERATE_TYPE_1;
  99. }
  100. if (methodName.startsWith("add")) {
  101. return CommonConstant.OPERATE_TYPE_2;
  102. }
  103. if (methodName.startsWith("edit")) {
  104. return CommonConstant.OPERATE_TYPE_3;
  105. }
  106. if (methodName.startsWith("delete")) {
  107. return CommonConstant.OPERATE_TYPE_4;
  108. }
  109. if (methodName.startsWith("import")) {
  110. return CommonConstant.OPERATE_TYPE_5;
  111. }
  112. if (methodName.startsWith("export")) {
  113. return CommonConstant.OPERATE_TYPE_6;
  114. }
  115. return CommonConstant.OPERATE_TYPE_1;
  116. }
  117. /**
  118. * 请求参数获取
  119. * @param request
  120. * @param joinPoint
  121. * @return
  122. */
  123. private String getReqestParams(HttpServletRequest request, JoinPoint joinPoint) {
  124. String httpMethod = request.getMethod();
  125. String params = "";
  126. if ("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod)) {
  127. Object[] paramsArray = joinPoint.getArgs();
  128. // java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
  129. // https://my.oschina.net/mengzhang6/blog/2395893
  130. Object[] arguments = new Object[paramsArray.length];
  131. for (int i = 0; i < paramsArray.length; i++) {
  132. if (paramsArray[i] instanceof BindingResult || paramsArray[i] instanceof ServletRequest || paramsArray[i] instanceof ServletResponse || paramsArray[i] instanceof MultipartFile) {
  133. //ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
  134. //ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
  135. continue;
  136. }
  137. arguments[i] = paramsArray[i];
  138. }
  139. //update-begin-author:taoyan date:20200724 for:日志数据太长的直接过滤掉
  140. PropertyFilter profilter = new PropertyFilter() {
  141. @Override
  142. public boolean apply(Object o, String name, Object value) {
  143. if(value!=null && value.toString().length()>500){
  144. return false;
  145. }
  146. return true;
  147. }
  148. };
  149. params = JSONObject.toJSONString(arguments, profilter);
  150. //update-end-author:taoyan date:20200724 for:日志数据太长的直接过滤掉
  151. } else {
  152. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  153. Method method = signature.getMethod();
  154. // 请求的方法参数值
  155. Object[] args = joinPoint.getArgs();
  156. // 请求的方法参数名称
  157. LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
  158. String[] paramNames = u.getParameterNames(method);
  159. if (args != null && paramNames != null) {
  160. for (int i = 0; i < args.length; i++) {
  161. params += " " + paramNames[i] + ": " + args[i];
  162. }
  163. }
  164. }
  165. return params;
  166. }
  167. }