bec640ed0a2e2d80634abb2bbd13d893da892e56.svn-base 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package cn.com.goldenwater.dcproj.target;
  2. import cn.com.goldenwater.core.web.BaseResponse;
  3. import cn.com.goldenwater.dcproj.constValue.CommonLabel;
  4. import cn.com.goldenwater.dcproj.model.BisInspLog;
  5. import cn.com.goldenwater.id.util.UuidUtil;
  6. import cn.com.goldenwater.util.common.IPUtils;
  7. import com.alibaba.fastjson.JSON;
  8. import io.swagger.annotations.ApiOperation;
  9. import net.sf.json.JSONObject;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.aspectj.lang.JoinPoint;
  12. import org.aspectj.lang.Signature;
  13. import org.aspectj.lang.annotation.AfterReturning;
  14. import org.aspectj.lang.annotation.AfterThrowing;
  15. import org.aspectj.lang.annotation.Aspect;
  16. import org.aspectj.lang.annotation.Pointcut;
  17. import org.aspectj.lang.reflect.MethodSignature;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.data.redis.core.RedisTemplate;
  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.lang.reflect.Method;
  27. import java.util.*;
  28. @Aspect
  29. @Component("logAspect")
  30. public class LogAspect {
  31. private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
  32. @Autowired
  33. private RedisTemplate redisTemplate;
  34. @Pointcut("execution(* cn.com.goldenwater.dcproj.controller..*(..)) && !execution(* cn.com.goldenwater.dcproj.controller.index..*(..))&& !execution(* cn.com.goldenwater.dcproj.controller.sso..*(..))&& !execution(* cn.com.goldenwater.dcproj.controller.ducha.MwrDataDownLoadController.*(..))&& !execution(* cn.com.goldenwater.dcproj.task..*(..))\"")
  35. public void logPointCut() {
  36. }
  37. /**
  38. * 前置通知 用于拦截操作,在方法返回后执行
  39. *
  40. * @param joinPoint 切点
  41. */
  42. @AfterReturning(pointcut = "logPointCut()", returning = "returnValue")
  43. public void doBefore(JoinPoint joinPoint, Object returnValue) {
  44. handleLog(joinPoint, null, returnValue);
  45. }
  46. /**
  47. * 拦截异常操作,有异常时执行
  48. *
  49. * @param joinPoint
  50. * @param e
  51. */
  52. @AfterThrowing(value = "logPointCut()", throwing = "e")
  53. public void doAfter(JoinPoint joinPoint, Exception e) {
  54. handleLog(joinPoint, e, null);
  55. }
  56. private void handleLog(JoinPoint joinPoint, Exception e, Object returnValue) {
  57. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  58. String persId = request.getHeader("persId");
  59. String orgId = request.getHeader("orgId");
  60. String source = request.getHeader("source");
  61. String uid = request.getHeader("uid");
  62. try {
  63. if (StringUtils.isBlank(persId)) {
  64. persId = "-1";
  65. }
  66. // 获得注解
  67. String reqPath = "";
  68. String method = request.getMethod();
  69. if (StringUtils.isBlank(reqPath)) {
  70. reqPath = request.getRequestURL().toString();
  71. if (reqPath.contains("/pers/position") || reqPath.contains("/grw/getScoreRule")
  72. || reqPath.contains("getCaptchaImage") || reqPath.contains("qrcode/login")
  73. || reqPath.contains("swagger") || reqPath.contains("/dc/insp/pblms/getPblmType")
  74. || reqPath.contains("/sys/privs/getPrivByUserId")
  75. ) {
  76. return;
  77. }
  78. }
  79. String content = "";
  80. if (StringUtils.isBlank(content)) {
  81. content = getAnnotationDesc(joinPoint);
  82. }
  83. Enumeration<String> enumeration = request.getParameterNames();
  84. Map<String, String> parameterMap = new HashMap<>();
  85. while (enumeration.hasMoreElements()) {
  86. String parameter = enumeration.nextElement();
  87. parameterMap.put(parameter, request.getParameter(parameter));
  88. }
  89. Map header = getHeadersInfo(request);
  90. if (StringUtils.isNotBlank(persId)) {
  91. BisInspLog bisInspLog = new BisInspLog();
  92. bisInspLog.setLogId(UuidUtil.uuid());
  93. bisInspLog.setContent(content);
  94. bisInspLog.setUserId(persId);
  95. bisInspLog.setPath(reqPath);
  96. bisInspLog.setOrgId(orgId);
  97. bisInspLog.setSource(source);
  98. bisInspLog.setMethod(method);
  99. bisInspLog.setHeader(JSON.toJSONString(header));
  100. bisInspLog.setRequestBody(Arrays.toString(joinPoint.getArgs()));
  101. if (returnValue instanceof BaseResponse) {
  102. BaseResponse baseResponse = (BaseResponse) returnValue;
  103. bisInspLog.setResponseBody(JSON.toJSONString(baseResponse));
  104. }
  105. bisInspLog.setIpAddr(IPUtils.getIpAddr(request));
  106. bisInspLog.setParams(JSON.toJSONString(parameterMap));
  107. bisInspLog.setAppModel(uid);
  108. bisInspLog.setIntm(new Date());
  109. JSONObject jsonObject = JSONObject.fromObject(bisInspLog);
  110. redisTemplate.convertAndSend(CommonLabel.channel, jsonObject);
  111. }
  112. } catch (Exception exp) {
  113. log.error("日志记录异常:{}", exp.getMessage(), exp);
  114. }
  115. }
  116. private static String getAnnotationDesc(JoinPoint joinPoint) {
  117. Signature signature = joinPoint.getSignature();
  118. MethodSignature methodSignature = (MethodSignature) signature;
  119. Method method = methodSignature.getMethod();
  120. if (method != null) {
  121. ApiOperation rm = method.getAnnotation(ApiOperation.class);
  122. if (rm != null) {
  123. return rm.value();
  124. }
  125. }
  126. return "";
  127. }
  128. private Map<String, String> getHeadersInfo(HttpServletRequest request) {
  129. Map<String, String> map = new HashMap<>(10);
  130. Enumeration headerNames = request.getHeaderNames();
  131. while (headerNames.hasMoreElements()) {
  132. String key = (String) headerNames.nextElement();
  133. String value = request.getHeader(key);
  134. map.put(key, value);
  135. }
  136. return map;
  137. }
  138. }