HttpAspect_CBIT.java 5.0 KB

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