Переглянути джерело

增加binlog敏感数据自动解密处理

woody 3 місяців тому
батько
коміт
550956fe21

+ 12 - 25
framework-base/src/main/java/com/chelvc/framework/base/crypto/CipherHandler.java

@@ -1,12 +1,9 @@
 package com.chelvc.framework.base.crypto;
 
-import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
 import com.chelvc.framework.common.util.ObjectUtils;
-import com.chelvc.framework.common.util.StringUtils;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import org.slf4j.Logger;
@@ -48,9 +45,6 @@ public interface CipherHandler {
      * @return 密文
      */
     default String encrypt(String plaintext, boolean force) {
-        if (StringUtils.isEmpty(plaintext)) {
-            return plaintext;
-        }
         try {
             return this.encrypt(plaintext);
         } catch (Exception e) {
@@ -119,25 +113,23 @@ public interface CipherHandler {
     /**
      * 数据加密
      *
-     * @param plaintexts 明文集合
+     * @param plaintexts 明文列表
      * @return 密文列表
      */
-    default List<String> encrypt(Collection<String> plaintexts) {
+    default List<String> encrypt(List<String> plaintexts) {
         return this.encrypt(plaintexts, false);
     }
 
     /**
      * 数据加密
      *
-     * @param plaintexts 明文集合
+     * @param plaintexts 明文列表
      * @param force      是否忽略异常
      * @return 密文列表
      */
-    default List<String> encrypt(Collection<String> plaintexts, boolean force) {
-        if (plaintexts == null) {
-            return null;
-        } else if (plaintexts.isEmpty()) {
-            return Collections.emptyList();
+    default List<String> encrypt(List<String> plaintexts, boolean force) {
+        if (ObjectUtils.isEmpty(plaintexts)) {
+            return plaintexts;
         }
         List<String> ciphertexts = Lists.newArrayListWithCapacity(plaintexts.size());
         plaintexts.forEach(plaintext -> ciphertexts.add(this.encrypt(plaintext, force)));
@@ -168,9 +160,6 @@ public interface CipherHandler {
      * @return 明文
      */
     default String decrypt(String ciphertext, boolean force) {
-        if (StringUtils.isEmpty(ciphertext)) {
-            return ciphertext;
-        }
         try {
             return this.decrypt(ciphertext);
         } catch (Exception e) {
@@ -239,25 +228,23 @@ public interface CipherHandler {
     /**
      * 数据解密
      *
-     * @param ciphertexts 密文集合
+     * @param ciphertexts 密文列表
      * @return 明文列表
      */
-    default List<String> decrypt(Collection<String> ciphertexts) {
+    default List<String> decrypt(List<String> ciphertexts) {
         return this.decrypt(ciphertexts, false);
     }
 
     /**
      * 数据解密
      *
-     * @param ciphertexts 密文集合
+     * @param ciphertexts 密文列表
      * @param force       是否忽略异常
      * @return 明文列表
      */
-    default List<String> decrypt(Collection<String> ciphertexts, boolean force) {
-        if (ciphertexts == null) {
-            return null;
-        } else if (ciphertexts.isEmpty()) {
-            return Collections.emptyList();
+    default List<String> decrypt(List<String> ciphertexts, boolean force) {
+        if (ObjectUtils.isEmpty(ciphertexts)) {
+            return ciphertexts;
         }
         List<String> plaintexts = Lists.newArrayListWithCapacity(ciphertexts.size());
         ciphertexts.forEach(ciphertext -> plaintexts.add(this.decrypt(ciphertext, force)));

+ 1 - 1
framework-database/src/main/java/com/chelvc/framework/database/interceptor/Expressions.java

@@ -193,7 +193,7 @@ final class Expressions {
      * @return 操作人/表达式
      */
     public static Pair<Long, Expression> operator() {
-        Long operator = ObjectUtils.ifNull(SessionContextHolder.getId(), 0L);
+        Long operator = ObjectUtils.ifNull(SessionContextHolder.getId(), 1L);
         return Pair.of(operator, new LongValue(operator));
     }
 

+ 15 - 40
framework-database/src/main/java/com/chelvc/framework/database/support/Binlog.java

@@ -11,6 +11,9 @@ import java.util.Objects;
 import com.chelvc.framework.common.util.JacksonUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
+import com.chelvc.framework.database.annotation.Sensitive;
+import com.chelvc.framework.database.context.DatabaseContextHolder;
+import com.chelvc.framework.database.handler.JsonTypeHandler;
 import com.google.common.collect.Maps;
 import lombok.AllArgsConstructor;
 import lombok.Data;
@@ -51,7 +54,7 @@ public class Binlog implements Serializable {
      * @param object 字段原始值
      * @return 转换后字段值
      */
-    public static Object convert(@NonNull Field field, Object object) {
+    private static Object convert(@NonNull Field field, Object object) {
         if (object == null) {
             return null;
         }
@@ -63,13 +66,17 @@ public class Binlog implements Serializable {
         }
         if (ObjectUtils.isMetaClass(type) || type.isEnum() || type == String.class
                 || Date.class.isAssignableFrom(type)) {
-            return JacksonUtils.convert(object, type);
+            Object value = JacksonUtils.convert(object, type);
+            if (type == String.class && field.isAnnotationPresent(Sensitive.class)) {
+                value = DatabaseContextHolder.getDatabaseCipherHandler().decrypt((String) value, true);
+            }
+            return value;
         } else if (object instanceof String && StringUtils.notEmpty(object)) {
             // binlog消息不支持JSON格式,所以需要单独反序列化处理
             String string = (String) object;
             char first = string.charAt(0), last = string.charAt(string.length() - 1);
             if ((first == '{' && last == '}') || (first == '[' && last == ']')) {
-                return JacksonUtils.deserialize(string, field.getGenericType());
+                return JacksonUtils.deserialize(JsonTypeHandler.MAPPER, string, field.getGenericType());
             }
         }
         return JacksonUtils.convert(object, type);
@@ -84,32 +91,20 @@ public class Binlog implements Serializable {
      * @return 实体对象实例
      */
     public static <T> T convert(@NonNull Class<T> model, Map<?, ?> mapping) {
-        T entity = ObjectUtils.instance(model);
-        update(entity, mapping);
-        return entity;
-    }
-
-    /**
-     * 更新实体属性值
-     *
-     * @param entity  实体对象实例
-     * @param mapping 字段名/值映射表
-     */
-    public static void update(@NonNull Object entity, Map<?, ?> mapping) {
         if (ObjectUtils.isEmpty(mapping)) {
-            return;
+            return null;
         }
 
-        Class<?> model = entity.getClass();
+        T instance = ObjectUtils.instance(model);
         Map<String, Field> fields = ObjectUtils.getClassFields(model);
         if (ObjectUtils.notEmpty(fields)) {
             for (Map.Entry<String, Field> entry : fields.entrySet()) {
                 String column = StringUtils.hump2underscore(entry.getKey());
-                Object value = mapping.get(column);
-                value = convert(entry.getValue(), value);
-                ObjectUtils.setObjectValue(entity, entry.getValue(), value);
+                Object value = convert(entry.getValue(), mapping.get(column));
+                ObjectUtils.setObjectValue(instance, entry.getValue(), value);
             }
         }
+        return instance;
     }
 
     /**
@@ -183,16 +178,6 @@ public class Binlog implements Serializable {
         return false;
     }
 
-    /**
-     * 赋值实体对象更新后属性值
-     *
-     * @param entity 实体对象实例
-     * @param <T>    实体类型
-     */
-    public <T> void after(@NonNull T entity) {
-        update(entity, this.after);
-    }
-
     /**
      * 赋值实体对象更新后属性值
      *
@@ -204,16 +189,6 @@ public class Binlog implements Serializable {
         return ObjectUtils.isEmpty(this.after) ? null : convert(model, this.after);
     }
 
-    /**
-     * 赋值实体对象更新前属性值
-     *
-     * @param entity 实体对象实例
-     * @param <T>    实体类型
-     */
-    public <T> void before(@NonNull T entity) {
-        update(entity, this.before);
-    }
-
     /**
      * 赋值实体对象更新前属性值
      *