浏览代码

字段加解密

gaodm 5 年之前
父节点
当前提交
b7a0c2adc4

+ 49 - 6
aipt-service/src/main/java/com/diagbot/aop/CryptAspect.java

@@ -2,6 +2,7 @@ package com.diagbot.aop;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.diagbot.client.bean.Response;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.util.CryptPojoUtils;
 import com.diagbot.util.RespDTOUtil;
@@ -22,19 +23,20 @@ import org.springframework.context.annotation.Configuration;
 @Configuration
 public class CryptAspect {
 
-    //切所有mapper
+    //切所有Controller
     @Pointcut("execution(* com.diagbot.web..*.*(..))")
-    public void pointcut() {
+    public void pointcutController() {
     }
 
-    @Around("pointcut()")
-    public Object aroundReturning(ProceedingJoinPoint joinPoint) throws Throwable {
+    //Controller 入参加密出参解密
+    @Around("pointcutController()")
+    public Object aroundReturningController(ProceedingJoinPoint joinPoint) throws Throwable {
         //获取方法参数
         Object[] args = joinPoint.getArgs();
 
-        //获取参数实体类注解
+        //获取参数实体类注解加密
         CryptPojoUtils.encryptFields(args);
-        //执行方法
+        //执行方法后获取出参
         Object proceed = joinPoint.proceed(args);
         if (null == proceed) {
             return null;
@@ -54,6 +56,47 @@ public class CryptAspect {
 
         }
 
+        return proceed;
+    }
+
+
+    @Pointcut("execution(* com.diagbot.client..*.*(..))")
+    public void pointcutRpcClient() {
+    }
+
+    //RpcClient 入参解密出参加密
+    @Around("pointcutRpcClient()")
+    public Object aroundReturningRpcClient(ProceedingJoinPoint joinPoint) throws Throwable {
+        //获取方法参数
+        Object[] args = joinPoint.getArgs();
+
+        //获取参数实体类解密
+        CryptPojoUtils.decryptFields(args);
+        //执行方法后获取出参
+        Object proceed = joinPoint.proceed(args);
+        if (null == proceed) {
+            return null;
+        }
+        //加密
+        if (proceed instanceof RespDTO) {
+            RespDTO respDTO = (RespDTO) proceed;
+            if (RespDTOUtil.respIsOK(respDTO)) {
+                if (respDTO.data instanceof IPage) {
+                    Page page = (Page) respDTO.data;
+                    CryptPojoUtils.encryptFieldOrList(page.getRecords());
+                } else {
+                    CryptPojoUtils.encryptFieldOrList(respDTO.data);
+                }
+
+            }
+        } else if (proceed instanceof Response) {
+            Response response = (Response) proceed;
+            if (null != response && null != response.getData()) {
+                CryptPojoUtils.encryptFieldOrList(response.getData());
+            }
+        }
+
+
         return proceed;
     }
 }

+ 11 - 2
common/src/main/java/com/diagbot/util/CryptPojoUtils.java

@@ -26,9 +26,9 @@ public class CryptPojoUtils {
     }
 
     /**
-     * 对含注解字段
+     * 对含注解字段
      */
-    private static <T> void encryptFieldOrList(T t) {
+    public static <T> void encryptFieldOrList(T t) {
         if (isNotCrypt(t)) {
             return;
         }
@@ -79,6 +79,15 @@ public class CryptPojoUtils {
         }
     }
 
+    /**
+     * 对含注解字段解密
+     */
+    public static <T> void decryptFields(T[] objects) {
+        for (Object obj : objects) {
+            decryptFieldOrList(obj);
+        }
+    }
+
     /**
      * 对含注解字段解密
      */

+ 49 - 6
knowledgeman-service/src/main/java/com/diagbot/aop/CryptAspect.java

@@ -2,6 +2,7 @@ package com.diagbot.aop;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.diagbot.client.bean.Response;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.util.CryptPojoUtils;
 import com.diagbot.util.RespDTOUtil;
@@ -22,19 +23,20 @@ import org.springframework.context.annotation.Configuration;
 @Configuration
 public class CryptAspect {
 
-    //切所有mapper
+    //切所有Controller
     @Pointcut("execution(* com.diagbot.web..*.*(..))")
-    public void pointcut() {
+    public void pointcutController() {
     }
 
-    @Around("pointcut()")
-    public Object aroundReturning(ProceedingJoinPoint joinPoint) throws Throwable {
+    //Controller 入参加密出参解密
+    @Around("pointcutController()")
+    public Object aroundReturningController(ProceedingJoinPoint joinPoint) throws Throwable {
         //获取方法参数
         Object[] args = joinPoint.getArgs();
 
-        //获取参数实体类注解
+        //获取参数实体类注解加密
         CryptPojoUtils.encryptFields(args);
-        //执行方法
+        //执行方法后获取出参
         Object proceed = joinPoint.proceed(args);
         if (null == proceed) {
             return null;
@@ -54,6 +56,47 @@ public class CryptAspect {
 
         }
 
+        return proceed;
+    }
+
+
+    @Pointcut("execution(* com.diagbot.client..*.*(..))")
+    public void pointcutRpcClient() {
+    }
+
+    //RpcClient 入参解密出参加密
+    @Around("pointcutRpcClient()")
+    public Object aroundReturningRpcClient(ProceedingJoinPoint joinPoint) throws Throwable {
+        //获取方法参数
+        Object[] args = joinPoint.getArgs();
+
+        //获取参数实体类解密
+        CryptPojoUtils.decryptFields(args);
+        //执行方法后获取出参
+        Object proceed = joinPoint.proceed(args);
+        if (null == proceed) {
+            return null;
+        }
+        //加密
+        if (proceed instanceof RespDTO) {
+            RespDTO respDTO = (RespDTO) proceed;
+            if (RespDTOUtil.respIsOK(respDTO)) {
+                if (respDTO.data instanceof IPage) {
+                    Page page = (Page) respDTO.data;
+                    CryptPojoUtils.encryptFieldOrList(page.getRecords());
+                } else {
+                    CryptPojoUtils.encryptFieldOrList(respDTO.data);
+                }
+
+            }
+        } else if (proceed instanceof Response) {
+            Response response = (Response) proceed;
+            if (null != response && null != response.getData()) {
+                CryptPojoUtils.encryptFieldOrList(response.getData());
+            }
+        }
+
+
         return proceed;
     }
 }