SmartOperateLogAspect.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package net.lab1024.smartadmin.handler;
  2. import com.alibaba.fastjson.JSON;
  3. import net.lab1024.smartadmin.common.anno.OperateLog;
  4. import net.lab1024.smartadmin.common.constant.JudgeEnum;
  5. import net.lab1024.smartadmin.module.business.log.LogService;
  6. import net.lab1024.smartadmin.module.business.log.useroperatelog.domain.UserOperateLogEntity;
  7. import net.lab1024.smartadmin.module.system.login.domain.RequestTokenBO;
  8. import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
  9. import net.lab1024.smartadmin.util.SmartStringUtil;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.aspectj.lang.JoinPoint;
  14. import org.aspectj.lang.Signature;
  15. import org.aspectj.lang.annotation.AfterReturning;
  16. import org.aspectj.lang.annotation.AfterThrowing;
  17. import org.aspectj.lang.annotation.Aspect;
  18. import org.aspectj.lang.annotation.Pointcut;
  19. import org.aspectj.lang.reflect.MethodSignature;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.core.annotation.AnnotationUtils;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.web.context.request.RequestContextHolder;
  24. import org.springframework.web.context.request.ServletRequestAttributes;
  25. import javax.servlet.http.HttpServletRequest;
  26. import java.io.PrintWriter;
  27. import java.io.StringWriter;
  28. import java.lang.reflect.Method;
  29. /**
  30. * [ 操作日志记录处理,对所有OperateLog注解的Controller进行操作日志监控 ]
  31. *
  32. * @author yandanyang
  33. * @version 1.0
  34. * @company 1024lab.net
  35. * @copyright (c) 2019 1024lab.netInc. All rights reserved.
  36. * @date
  37. * @since JDK1.8
  38. */
  39. @Slf4j
  40. @Aspect
  41. @Component
  42. public class SmartOperateLogAspect {
  43. @Autowired
  44. private LogService logService;
  45. @Pointcut("execution(* net.lab1024.smartadmin.module..*Controller.*(..)))")
  46. public void logPointCut() {
  47. }
  48. @AfterReturning(pointcut = "logPointCut()")
  49. public void doAfterReturning(JoinPoint joinPoint) {
  50. handleLog(joinPoint, null);
  51. }
  52. @AfterThrowing(value = "logPointCut()", throwing = "e")
  53. public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
  54. handleLog(joinPoint, e);
  55. }
  56. protected void handleLog(final JoinPoint joinPoint, final Exception e) {
  57. try {
  58. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  59. OperateLog operateLog = this.getAnnotationLog(joinPoint);
  60. if (operateLog == null) {
  61. return;
  62. }
  63. RequestTokenBO requestToken = SmartRequestTokenUtil.getRequestUser();
  64. if (requestToken == null) {
  65. return;
  66. }
  67. // 设置方法名称
  68. String className = joinPoint.getTarget().getClass().getName();
  69. String methodName = joinPoint.getSignature().getName();
  70. String operateMethod = className + "." + methodName;
  71. Object[] args = joinPoint.getArgs();
  72. StringBuilder sb = new StringBuilder();
  73. for (Object obj : args) {
  74. sb.append(obj.getClass().getSimpleName());
  75. sb.append("[");
  76. sb.append(JSON.toJSONString(obj));
  77. sb.append("]");
  78. }
  79. String params = sb.toString();
  80. String failReason = null;
  81. Integer result = JudgeEnum.YES.getValue();
  82. if (e != null) {
  83. result = JudgeEnum.NO.getValue();
  84. StringWriter sw = new StringWriter();
  85. PrintWriter pw = new PrintWriter(sw, true);
  86. e.printStackTrace(pw);
  87. failReason = sw.toString();
  88. pw.flush();
  89. pw.close();
  90. sw.flush();
  91. sw.close();
  92. }
  93. UserOperateLogEntity operateLogEntity =
  94. UserOperateLogEntity.builder().userId(requestToken.getRequestUserId()).userName(requestToken.getEmployeeBO().getActualName()).url(request.getRequestURI()).method(operateMethod).param(params).failReason(failReason).result(result).build();
  95. ApiOperation apiOperation = this.getApiOperation(joinPoint);
  96. if (apiOperation != null) {
  97. operateLogEntity.setContent(apiOperation.value());
  98. }
  99. Api api = this.getApi(joinPoint);
  100. if (api != null) {
  101. String[] tags = api.tags();
  102. operateLogEntity.setModule(SmartStringUtil.join(tags, ","));
  103. }
  104. logService.addLog(operateLogEntity);
  105. } catch (Exception exp) {
  106. log.error("保存操作日志异常:{}", exp.getMessage());
  107. exp.printStackTrace();
  108. }
  109. }
  110. private OperateLog getAnnotationLog(JoinPoint joinPoint) throws Exception {
  111. Signature signature = joinPoint.getSignature();
  112. MethodSignature methodSignature = (MethodSignature) signature;
  113. Method method = methodSignature.getMethod();
  114. OperateLog classAnnotation = AnnotationUtils.findAnnotation(method.getDeclaringClass(), OperateLog.class);
  115. if (method != null) {
  116. return classAnnotation;
  117. }
  118. return null;
  119. }
  120. /**
  121. * swagger API
  122. *
  123. * @param joinPoint
  124. * @return
  125. * @throws Exception
  126. */
  127. private Api getApi(JoinPoint joinPoint) {
  128. Signature signature = joinPoint.getSignature();
  129. MethodSignature methodSignature = (MethodSignature) signature;
  130. Method method = methodSignature.getMethod();
  131. Api classAnnotation = AnnotationUtils.findAnnotation(method.getDeclaringClass(), Api.class);
  132. if (method != null) {
  133. return classAnnotation;
  134. }
  135. return null;
  136. }
  137. /**
  138. * swagger ApiOperation
  139. *
  140. * @param joinPoint
  141. * @return
  142. * @throws Exception
  143. */
  144. private ApiOperation getApiOperation(JoinPoint joinPoint) {
  145. Signature signature = joinPoint.getSignature();
  146. MethodSignature methodSignature = (MethodSignature) signature;
  147. Method method = methodSignature.getMethod();
  148. if (method != null) {
  149. return method.getAnnotation(ApiOperation.class);
  150. }
  151. return null;
  152. }
  153. }