ServiceAspect.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.dk.common.infrastructure.aspect;
  2. import com.dk.common.exception.BaseBusinessException;
  3. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  4. import com.dk.common.infrastructure.util.AuthUtils;
  5. import com.dk.common.infrastructure.util.MyMetaObjectHandler;
  6. import com.dk.common.infrastructure.util.MybatisSqlIntercept;
  7. import com.dk.common.mapper.opinfo.OpInfoMapper;
  8. import com.dk.common.infrastructure.operationlog.OperationLogService;
  9. import com.dk.common.model.vo.core.UserVO;
  10. import com.dk.common.util.oauth.AESSecurityUtil;
  11. import com.dk.common.util.oauth.JwtUtil;
  12. import org.aspectj.lang.JoinPoint;
  13. import org.aspectj.lang.annotation.After;
  14. import org.aspectj.lang.annotation.Aspect;
  15. import org.aspectj.lang.annotation.Before;
  16. import org.aspectj.lang.annotation.Pointcut;
  17. import org.aspectj.lang.reflect.MethodSignature;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.web.servlet.HandlerInterceptor;
  22. import javax.servlet.http.HttpServletRequest;
  23. import java.lang.reflect.Method;
  24. import java.util.*;
  25. /**
  26. * @author : 张潇木
  27. * @desc : 处理数据库通用字段op_create_user_id、op_update_user_id、op_app_id、op_api_id的切面
  28. * @date : 2022-3-1 12:49
  29. */
  30. @Aspect
  31. @Component
  32. public class ServiceAspect implements HandlerInterceptor {
  33. @Value("${aes-key}")
  34. private String AESKey;
  35. @Autowired
  36. private OpInfoMapper opInfoMapper;
  37. @Autowired
  38. private HttpServletRequest httpServletRequest;
  39. @Autowired
  40. private OperationLogService operationLogService;
  41. @Autowired
  42. private AuthUtils authUtils;
  43. /**
  44. * @desc : 切点
  45. * @author : 张潇木
  46. * @date : 2022-3-1 12:49
  47. */
  48. @Pointcut("execution(* com.*.*.service..*.*(..)) || execution(* com.dk.*.service..*.*(..)) ||" +
  49. "execution(* com.dk.*.infrastructure.strategy.ProductionContext.*(..)) ||" +
  50. "execution(* com.dk.*.infrastructure.strategy.production.ProductionContext.*(..))" +
  51. "&&!execution(* com.dk.*.service.wxapi.basic.WechatPayService.*(..))")
  52. public void print() {
  53. }
  54. /**
  55. * @desc : service前置切面 调用共通函数,在数据库中设置当前操作的appid,apiid,userid,存到数据库session中。
  56. * 保存数据时,触发器会从session中读取该信息。自动处理op_create_user_id、op_update_user_id、op_app_id、op_api_id
  57. * @author : 张潇木
  58. * @date : 2022-3-1 12:49
  59. */
  60. @Before("print()")
  61. public void before(JoinPoint joinPoint) {
  62. try {
  63. String authorization = httpServletRequest.getHeader("Authorization");
  64. if(authorization == null ){
  65. return;
  66. }
  67. String[] tokens = authorization.split(" ");
  68. String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]);
  69. String userId = JwtUtil.getUserId(decrypt);
  70. String appCode = JwtUtil.getAppCode(decrypt);
  71. //查询及其他特殊方法跳过
  72. if (!joinPoint.getSignature().getName().startsWith("select") &&
  73. !joinPoint.getSignature().getName().startsWith("get") &&
  74. !joinPoint.getSignature().getName().equals("login") &&
  75. authorization != null
  76. ) {
  77. opInfoMapper.setOpInfo(appCode, userId.toString(), JwtUtil.getLang(decrypt));
  78. //记录修改前数据
  79. if (!skipMethod.contains(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName())) {
  80. operationLogService.saveLogBefore(joinPoint);
  81. }
  82. }
  83. // MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  84. //
  85. // // 获取当前类名
  86. // String className = signature.getDeclaringTypeName();
  87. // String methodName = "getPrimaryKey";
  88. // Class clz = Class.forName(className);
  89. // Object obj = clz.newInstance();
  90. // //获取方法
  91. // Method m = obj.getClass().getDeclaredMethod(methodName);
  92. // //调用方法
  93. // String primaryKey = (String) m.invoke(obj);
  94. // /* 从请求头中获取主键Id,用于用Id进行查询的是否加入语言和公司 */
  95. // MybatisSqlIntercept.primaryKey = httpServletRequest.getHeader("primaryKey");
  96. /* 从请求头中获取国际化参数 */
  97. MybatisSqlIntercept.I18N = httpServletRequest.getHeader("i18n");
  98. // opInfoMapper.setOpInfo("","","");
  99. } catch (Exception ignored) {
  100. // 支付的回调的时候报错240327
  101. try{
  102. opInfoMapper.setOpInfo(UUID.randomUUID().toString(), "10002024-0218-0000-0000-0000000b8b9b", httpServletRequest.getHeader("i18n"));
  103. }catch (Exception e){
  104. opInfoMapper.setOpInfo(UUID.randomUUID().toString(), "10002024-0218-0000-0000-0000000b8b9b", "zh_CN");
  105. }
  106. }
  107. }
  108. /**
  109. * @desc : 后置切面
  110. * @author : 洪旭东
  111. * @date : 2023-08-16 15:16
  112. */
  113. @After("print()")
  114. public void after(JoinPoint joinPoint) {
  115. //查询及登录方法跳过
  116. if (!joinPoint.getSignature().getName().startsWith("select") &&
  117. !joinPoint.getSignature().getName().startsWith("get") &&
  118. !joinPoint.getSignature().getName().equals("login") &&
  119. !joinPoint.getSignature().getDeclaringTypeName().equals("com.dk.oauth.service.impl.AuthAccessTokenServiceImpl")
  120. ) {
  121. // UserVO user = authUtils.getUser();
  122. // if (user!=null && !skipMethod.contains(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName())) {
  123. // operationLogService.saveLogAfter(joinPoint, httpServletRequest.getAttribute("requestUuid"), user.getFtyId());
  124. // }
  125. }
  126. }
  127. /**
  128. * @desc : 不记录日志的方法
  129. * @author : 洪旭东
  130. * @date : 2023-09-05 13:54
  131. */
  132. private final List<String> skipMethod = new ArrayList<String>() {{
  133. add("com.dk.common.service.BaseService.insert");
  134. add("com.dk.mdm.service.pdm.WsClockService.auto");
  135. }};
  136. }