Procházet zdrojové kódy

尝试解决多线程cpId混乱

zhangxiaomu před 1 rokem
rodič
revize
b7ff46ca13

+ 0 - 11
src/main/java/com/dk/common/infrastructure/aspect/ServiceAspect.java

@@ -92,17 +92,6 @@ public class ServiceAspect implements HandlerInterceptor {
                 if (!skipMethod.contains(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName())) {
                 if (!skipMethod.contains(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName())) {
                     operationLogService.saveLogBefore(joinPoint);
                     operationLogService.saveLogBefore(joinPoint);
                 }
                 }
-                // 设置企业Id
-                String cpId = JwtUtil.getCpId(decrypt);
-                if (cpId != null ) {
-                    MyMetaObjectHandler.cpId = Integer.parseInt(cpId);
-                }
-            }else{
-                // 设置企业Id
-                String cpId = JwtUtil.getCpId(decrypt);
-                if (cpId != null && joinPoint.getSignature().getName() != "getFeignExperience") {
-                    MybatisSqlIntercept.cpId = Integer.parseInt(cpId);
-                }
             }
             }
 
 
 //            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
 //            MethodSignature signature = (MethodSignature) joinPoint.getSignature();

+ 27 - 2
src/main/java/com/dk/common/infrastructure/util/MyMetaObjectHandler.java

@@ -1,23 +1,48 @@
 package com.dk.common.infrastructure.util;
 package com.dk.common.infrastructure.util;
 
 
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+import com.dk.common.util.oauth.AESSecurityUtil;
+import com.dk.common.util.oauth.JwtUtil;
 import groovy.util.logging.Slf4j;
 import groovy.util.logging.Slf4j;
+import lombok.SneakyThrows;
 import org.apache.ibatis.reflection.MetaObject;
 import org.apache.ibatis.reflection.MetaObject;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 
 
+import javax.servlet.http.HttpServletRequest;
+
 @Slf4j
 @Slf4j
 @Component
 @Component
 public class MyMetaObjectHandler implements MetaObjectHandler {
 public class MyMetaObjectHandler implements MetaObjectHandler {
 
 
-    public static Integer cpId = 0;
+    @Value("${aes-key}")
+    private String AESKey;
+
+    @Autowired
+    private HttpServletRequest httpServletRequest;
 
 
     @Override
     @Override
+    @SneakyThrows
     public void insertFill(MetaObject metaObject) {
     public void insertFill(MetaObject metaObject) {
-        this.strictInsertFill(metaObject, "cpId", Integer.class, cpId); // 起始版本 3.3.0(推荐使用)
+        String authorization = httpServletRequest.getHeader("Authorization");
+
+        if(authorization != null){
+
+            String[] tokens = authorization.split(" ");
+            String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]);
+            String cpIdString = JwtUtil.getCpId(decrypt);
+            if (cpIdString!=null){
+                //设置企业Id
+                Integer cpId = Integer.valueOf(cpIdString);
+                this.strictInsertFill(metaObject, "cpId", Integer.class, cpId); // 起始版本 3.3.0(推荐使用)
+            }
 //        // 或者
 //        // 或者
 //        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
 //        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
 //        // 或者
 //        // 或者
 //        this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)
 //        this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)
+        }
+
     }
     }
 
 
     @Override
     @Override

+ 28 - 1
src/main/java/com/dk/common/infrastructure/util/MybatisSqlIntercept.java

@@ -2,6 +2,8 @@ package com.dk.common.infrastructure.util;
 
 
 
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSON;
+import com.dk.common.util.oauth.AESSecurityUtil;
+import com.dk.common.util.oauth.JwtUtil;
 import com.google.common.base.CaseFormat;
 import com.google.common.base.CaseFormat;
 import org.apache.ibatis.builder.StaticSqlSource;
 import org.apache.ibatis.builder.StaticSqlSource;
 import org.apache.ibatis.executor.Executor;
 import org.apache.ibatis.executor.Executor;
@@ -15,8 +17,11 @@ import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
 import org.apache.ibatis.scripting.defaults.RawSqlSource;
 import org.apache.ibatis.scripting.defaults.RawSqlSource;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.RowBounds;
 import org.apache.ibatis.session.RowBounds;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 
 
+import javax.servlet.http.HttpServletRequest;
 import java.beans.BeanInfo;
 import java.beans.BeanInfo;
 import java.beans.Introspector;
 import java.beans.Introspector;
 import java.beans.PropertyDescriptor;
 import java.beans.PropertyDescriptor;
@@ -41,11 +46,33 @@ import java.util.Map;
 public class MybatisSqlIntercept implements Interceptor {
 public class MybatisSqlIntercept implements Interceptor {
 
 
     public static String I18N = "";
     public static String I18N = "";
-    public static Integer cpId = 0;
 //    public static String primaryKey = null;
 //    public static String primaryKey = null;
 
 
+    @Value("${aes-key}")
+    private String AESKey;
+
+    @Autowired
+    private HttpServletRequest httpServletRequest;
+
     @Override
     @Override
     public Object intercept(Invocation invocation) throws Throwable {
     public Object intercept(Invocation invocation) throws Throwable {
+
+        Integer cpId = 0;
+
+        String authorization = httpServletRequest.getHeader("Authorization");
+
+        if(authorization != null) {
+
+            String[] tokens = authorization.split(" ");
+            String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]);
+            String cpIdString = JwtUtil.getCpId(decrypt);
+            if (cpIdString != null) {
+                //设置企业Id
+                cpId = Integer.valueOf(cpIdString);
+            }
+
+        }
+
         Object[] args = invocation.getArgs();
         Object[] args = invocation.getArgs();
         Object[] args2 = new Object[4];
         Object[] args2 = new Object[4];
         args2[3] = args[3];
         args2[3] = args[3];