LogAspect.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.ydtech.aop;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.ydtech.core.page.HttpResult;
  4. import com.ydtech.modules.admin.model.SysLog;
  5. import com.ydtech.modules.admin.service.SysLogService;
  6. import com.ydtech.security.utils.JwtTokenUtils;
  7. import io.swagger.annotations.ApiOperation;
  8. import org.aspectj.lang.JoinPoint;
  9. import org.aspectj.lang.ProceedingJoinPoint;
  10. import org.aspectj.lang.annotation.*;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.web.context.request.RequestContextHolder;
  16. import org.springframework.web.context.request.ServletRequestAttributes;
  17. import javax.servlet.http.HttpServletRequest;
  18. import java.lang.reflect.Method;
  19. import java.util.Date;
  20. /**
  21. * 日志切面类
  22. * AOP切面,记录方法的调用,入参以及出參
  23. */
  24. @Aspect
  25. @Component
  26. public class LogAspect {
  27. private final static Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
  28. @Autowired
  29. private GlobalExceptionHandler exceptionHandle;
  30. @Autowired
  31. private SysLogService sysLogService;
  32. @Pointcut("execution(public * com.ydtech.modules.*.controller.*.*(..)) ")
  33. public void log() {
  34. }
  35. @Before("log()")
  36. public void doBefore(JoinPoint joinPoint) {
  37. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  38. HttpServletRequest request = attributes.getRequest();
  39. //请求人 url method ip class_method
  40. SysLog sysLog = new SysLog(JwtTokenUtils.getUsernameFromRequest(request), request.getRequestURL().toString(), request.getMethod(), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), request.getRemoteAddr(), new Date());
  41. try {
  42. //获取注解注释
  43. String annotationValue = getAnnotationValue(joinPoint);
  44. sysLog.setDescription(annotationValue);
  45. //获取请求参数
  46. Object[] args = joinPoint.getArgs();
  47. for (Object obj : args) {
  48. if (null != obj && (obj.getClass().getName().startsWith("com.ydtech.modules") || obj instanceof String)) {
  49. sysLog.setParams(JSONObject.toJSON(obj).toString());
  50. }
  51. }
  52. if (request.getMethod().equals("GET")) {
  53. sysLog.setParams(request.getQueryString());
  54. }
  55. sysLog.preInsert();
  56. sysLogService.insert(sysLog);
  57. } catch (ClassNotFoundException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. @Around("log()")
  62. public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  63. HttpResult result = null;
  64. try {
  65. } catch (Exception e) {
  66. return exceptionHandle.exceptionGet(e);
  67. }
  68. if (result == null) {
  69. return proceedingJoinPoint.proceed();
  70. } else {
  71. return result;
  72. }
  73. }
  74. @AfterReturning(pointcut = "log()", returning = "object")//打印输出结果
  75. public void doAfterReturing(Object object) {
  76. if (object != null) {
  77. LOGGER.info("response={}", object.toString());
  78. }
  79. }
  80. private String getAnnotationValue(JoinPoint joinPoint) throws ClassNotFoundException {
  81. //反射获取类
  82. Class<?> clazz = Class.forName(joinPoint.getSignature().getDeclaringTypeName());
  83. //通过方法名反射获取method
  84. Method[] declaredMethods = clazz.getDeclaredMethods();
  85. //请求方法名
  86. String requestName = joinPoint.getSignature().getName();
  87. for (Method method : declaredMethods) {
  88. String name = method.getName();
  89. //获取方法上的注解
  90. if (name.equals(requestName)) {
  91. ApiOperation annotation = method.getAnnotation(ApiOperation.class);
  92. if (annotation != null) {
  93. return annotation.value();
  94. }
  95. }
  96. }
  97. return null;
  98. }
  99. }