| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package com.dk.common.infrastructure.aspect;
- import com.dk.common.exception.BaseBusinessException;
- import com.dk.common.infrastructure.enums.ErrorCodeEnum;
- import com.dk.common.infrastructure.util.AuthUtils;
- import com.dk.common.infrastructure.util.MyMetaObjectHandler;
- import com.dk.common.infrastructure.util.MybatisSqlIntercept;
- import com.dk.common.mapper.opinfo.OpInfoMapper;
- import com.dk.common.infrastructure.operationlog.OperationLogService;
- import com.dk.common.model.vo.core.UserVO;
- import com.dk.common.util.oauth.AESSecurityUtil;
- import com.dk.common.util.oauth.JwtUtil;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.aspectj.lang.reflect.MethodSignature;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import org.springframework.web.servlet.HandlerInterceptor;
- import javax.servlet.http.HttpServletRequest;
- import java.lang.reflect.Method;
- import java.util.*;
- /**
- * @author : 张潇木
- * @desc : 处理数据库通用字段op_create_user_id、op_update_user_id、op_app_id、op_api_id的切面
- * @date : 2022-3-1 12:49
- */
- @Aspect
- @Component
- public class ServiceAspect implements HandlerInterceptor {
- @Value("${aes-key}")
- private String AESKey;
- @Autowired
- private OpInfoMapper opInfoMapper;
- @Autowired
- private HttpServletRequest httpServletRequest;
- @Autowired
- private OperationLogService operationLogService;
- @Autowired
- private AuthUtils authUtils;
- /**
- * @desc : 切点
- * @author : 张潇木
- * @date : 2022-3-1 12:49
- */
- @Pointcut("execution(* com.*.*.service..*.*(..)) || execution(* com.dk.*.service..*.*(..)) ||" +
- "execution(* com.dk.*.infrastructure.strategy.ProductionContext.*(..)) ||" +
- "execution(* com.dk.*.infrastructure.strategy.production.ProductionContext.*(..))" +
- "&&!execution(* com.dk.*.service.wxapi.basic.WechatPayService.*(..))")
- public void print() {
- }
- /**
- * @desc : service前置切面 调用共通函数,在数据库中设置当前操作的appid,apiid,userid,存到数据库session中。
- * 保存数据时,触发器会从session中读取该信息。自动处理op_create_user_id、op_update_user_id、op_app_id、op_api_id
- * @author : 张潇木
- * @date : 2022-3-1 12:49
- */
- @Before("print()")
- public void before(JoinPoint joinPoint) {
- try {
- String authorization = httpServletRequest.getHeader("Authorization");
- if(authorization == null ){
- return;
- }
- String[] tokens = authorization.split(" ");
- String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]);
- String userId = JwtUtil.getUserId(decrypt);
- String appCode = JwtUtil.getAppCode(decrypt);
- //查询及其他特殊方法跳过
- if (!joinPoint.getSignature().getName().startsWith("select") &&
- !joinPoint.getSignature().getName().startsWith("get") &&
- !joinPoint.getSignature().getName().equals("login") &&
- authorization != null
- ) {
- opInfoMapper.setOpInfo(appCode, userId.toString(), JwtUtil.getLang(decrypt));
- //记录修改前数据
- if (!skipMethod.contains(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName())) {
- operationLogService.saveLogBefore(joinPoint);
- }
- }
- // MethodSignature signature = (MethodSignature) joinPoint.getSignature();
- //
- // // 获取当前类名
- // String className = signature.getDeclaringTypeName();
- // String methodName = "getPrimaryKey";
- // Class clz = Class.forName(className);
- // Object obj = clz.newInstance();
- // //获取方法
- // Method m = obj.getClass().getDeclaredMethod(methodName);
- // //调用方法
- // String primaryKey = (String) m.invoke(obj);
- // /* 从请求头中获取主键Id,用于用Id进行查询的是否加入语言和公司 */
- // MybatisSqlIntercept.primaryKey = httpServletRequest.getHeader("primaryKey");
- /* 从请求头中获取国际化参数 */
- MybatisSqlIntercept.I18N = httpServletRequest.getHeader("i18n");
- // opInfoMapper.setOpInfo("","","");
- } catch (Exception ignored) {
- // 支付的回调的时候报错240327
- try{
- opInfoMapper.setOpInfo(UUID.randomUUID().toString(), "10002024-0218-0000-0000-0000000b8b9b", httpServletRequest.getHeader("i18n"));
- }catch (Exception e){
- opInfoMapper.setOpInfo(UUID.randomUUID().toString(), "10002024-0218-0000-0000-0000000b8b9b", "zh_CN");
- }
- }
- }
- /**
- * @desc : 后置切面
- * @author : 洪旭东
- * @date : 2023-08-16 15:16
- */
- @After("print()")
- public void after(JoinPoint joinPoint) {
- //查询及登录方法跳过
- if (!joinPoint.getSignature().getName().startsWith("select") &&
- !joinPoint.getSignature().getName().startsWith("get") &&
- !joinPoint.getSignature().getName().equals("login") &&
- !joinPoint.getSignature().getDeclaringTypeName().equals("com.dk.oauth.service.impl.AuthAccessTokenServiceImpl")
- ) {
- // UserVO user = authUtils.getUser();
- // if (user!=null && !skipMethod.contains(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName())) {
- // operationLogService.saveLogAfter(joinPoint, httpServletRequest.getAttribute("requestUuid"), user.getFtyId());
- // }
- }
- }
- /**
- * @desc : 不记录日志的方法
- * @author : 洪旭东
- * @date : 2023-09-05 13:54
- */
- private final List<String> skipMethod = new ArrayList<String>() {{
- add("com.dk.common.service.BaseService.insert");
- add("com.dk.mdm.service.pdm.WsClockService.auto");
- }};
- }
|