From 93b505ef316dcb0b22bac49aea975a8302dcb3ec Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期二, 18 一月 2022 14:14:25 +0800
Subject: [PATCH] 新增数据库统一加密工具

---
 src/main/java/com/hx/common/xml/CommonMapper.xml                |   14 ++++
 src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java  |  108 ++++++++++++++++++++++++++++++++++++
 src/main/java/com/hx/common/service/impl/CommonServiceImpl.java |    8 ++
 src/main/java/com/hx/common/service/impl/CommonDaoImpl.java     |    8 ++
 src/main/java/com/hx/common/dao/CommonMapper.java               |   17 +++++
 src/main/java/com/hx/common/service/CommonService.java          |    9 ++
 src/main/java/com/hx/common/dao/CommonDao.java                  |    9 ++
 7 files changed, 171 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/hx/common/dao/CommonDao.java b/src/main/java/com/hx/common/dao/CommonDao.java
index 3a4ccad..3a5c32d 100644
--- a/src/main/java/com/hx/common/dao/CommonDao.java
+++ b/src/main/java/com/hx/common/dao/CommonDao.java
@@ -103,5 +103,12 @@
 	 * @return 杩斿洖鏉℃暟
 	 */
 	<T extends Serializable> int deleteById(Class<?> mapperClass, Object object);
-	
+
+	/**鏇存柊sql璇彞锛堝叏璇彞锛�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int updateSentence( SqlSentence sqlSentence);
+
+
 }
\ No newline at end of file
diff --git a/src/main/java/com/hx/common/dao/CommonMapper.java b/src/main/java/com/hx/common/dao/CommonMapper.java
new file mode 100644
index 0000000..4b29de2
--- /dev/null
+++ b/src/main/java/com/hx/common/dao/CommonMapper.java
@@ -0,0 +1,17 @@
+package com.hx.common.dao;
+
+import com.hx.mybatisTool.SqlSentence;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.Map;
+
+public interface CommonMapper {
+
+    /**鏇存柊锛岃繑鍥炴洿鏂版暟閲�*/
+    int updateSentence(SqlSentence sqlSentence);
+
+    /**鏌ヨ鍒楄〃锛岃繑鍥濵ap鐨凩ist*/
+    List<Map<String,Object>> selectListMap(SqlSentence sqlSentence);
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/hx/common/service/CommonService.java b/src/main/java/com/hx/common/service/CommonService.java
index 2d35f9f..312786c 100644
--- a/src/main/java/com/hx/common/service/CommonService.java
+++ b/src/main/java/com/hx/common/service/CommonService.java
@@ -104,5 +104,12 @@
 	 * @return 杩斿洖鏉℃暟
 	 */
 	<T extends Serializable> int deleteById(Class<?> mapperClass, Object object);
-	
+
+	/**鏇存柊sql璇彞锛堝叏璇彞锛�
+	 * @param sqlSentence 鏌ヨ鍙傛暟绫�
+	 * @return 杩斿洖鏉℃暟
+	 */
+	<T extends Serializable> int updateSentence(SqlSentence sqlSentence);
+
+
 }
\ No newline at end of file
diff --git a/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java
index 16ef362..685e468 100644
--- a/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java
+++ b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java
@@ -1,6 +1,7 @@
 package com.hx.common.service.impl;
 
 import com.hx.common.dao.CommonDao;
+import com.hx.common.dao.CommonMapper;
 import com.hx.mybatisTool.SqlSentence;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.springframework.stereotype.Service;
@@ -102,4 +103,11 @@
         return sqlSessionFactory.openSession().delete(getStatement(mapperClass,"deleteById"),object);
     }
 
+    /**鏇存柊sql璇彞锛堝叏璇彞锛�*/
+    @Override
+    public <T extends Serializable> int updateSentence(SqlSentence sqlSentence) {
+        return sqlSessionFactory.openSession().delete(getStatement(CommonMapper.class,"updateSentence"),sqlSentence);
+    }
+
+
 }
diff --git a/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java
index c2822ec..078f8d2 100644
--- a/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java
+++ b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java
@@ -97,4 +97,12 @@
         return commonDao.deleteById(mapperClass,object);
     }
 
+
+    /**鏇存柊sql璇彞锛堝叏璇彞锛�*/
+    @Override
+    public <T extends Serializable> int updateSentence(SqlSentence sqlSentence) {
+        return commonDao.updateSentence(sqlSentence);
+    }
+
+
 }
diff --git a/src/main/java/com/hx/common/xml/CommonMapper.xml b/src/main/java/com/hx/common/xml/CommonMapper.xml
new file mode 100644
index 0000000..968338a
--- /dev/null
+++ b/src/main/java/com/hx/common/xml/CommonMapper.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<!-- namespace:璇apper.xml鏄犲皠鏂囦欢鐨� 鍞竴鏍囪瘑 -->
+<mapper namespace="com.hx.common.dao.CommonMapper">
+	<update id="updateSentence" parameterType="com.hx.mybatisTool.SqlSentence" >
+		 ${sqlSentence}
+	</update>
+
+	<select id="selectListMap" resultType="java.util.Map" parameterType="com.hx.mybatisTool.SqlSentence" >
+		${sqlSentence}
+	</select>
+</mapper>
\ No newline at end of file
diff --git a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
new file mode 100644
index 0000000..2c5abcc
--- /dev/null
+++ b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
@@ -0,0 +1,108 @@
+package com.hx.mybatis.aes.springbean;
+
+import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
+import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
+import com.hx.common.annotations.MysqlHexAes;
+import com.hx.common.dao.CommonMapper;
+import com.hx.common.service.CommonService;
+import com.hx.exception.ServiceException;
+import com.hx.mybatisTool.SqlSentence;
+import com.hx.util.StringUtils;
+import com.hx.util.mysql.aes.MysqlHexAesTool;
+
+import javax.annotation.PostConstruct;
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class InitMysqlData {
+
+    /**
+     * 椤圭洰鍚姩灏辨墽琛屽悗灏辨墽琛岃鏂规硶
+     */
+    @PostConstruct
+    public static void initData(String packPath, CommonService commonService){
+
+        //椤圭洰鍚姩鐨勬椂鍊欏~鍏�
+        if(!StringUtils.isEmpty(packPath)){
+            Set<Class<?>> classes = VariableAesKey.classData(packPath);
+            Map<String,String> aesKeysFild = new HashMap<>();
+            boolean isAes = false;
+            String tableName = null;
+            String fildName = null;
+            String fildValue = null;
+
+            SqlSentence sqlSentence = new SqlSentence();
+            Map<String,Object> values = new HashMap<>();
+
+            for(Class<?> cl:classes){
+                //琛ㄥ悕绉�
+                boolean hasAnnotation = cl.isAnnotationPresent(Table.class);
+                if(!hasAnnotation){
+                    continue;
+                }
+                Table table = cl.getAnnotation(Table.class);
+                tableName = table.name();
+
+                aesKeysFild = new HashMap<>();
+                isAes = false;
+
+                // 鍙栧緱鏈被鐨勫叏閮ㄥ睘鎬�
+                Field[] fields = cl.getDeclaredFields();
+                fields = VariableAesKey.getPatentFields(fields,cl);
+                for (Field field:fields) {
+                    fildName = null;
+                    // 鍒ゆ柇鏂规硶涓槸鍚︽湁鎸囧畾娉ㄨВ绫诲瀷鐨勬敞瑙�
+                    hasAnnotation = field.isAnnotationPresent(MysqlHexAes.class);
+                    if (hasAnnotation) {
+                        // 鏍规嵁娉ㄨВ绫诲瀷杩斿洖鏂规硶鐨勬寚瀹氱被鍨嬫敞瑙�
+                        MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
+
+                        //String aesKeyField = mysqlHexAes.aesKeyField();
+                        String aesKey = mysqlHexAes.aesKey();
+
+                        if(StringUtils.isEmpty(aesKey)){
+                            aesKey = VariableAesKey.AES_KEY;
+                            if(StringUtils.isEmpty(aesKey)){
+                                throw new RuntimeException("mysql鐨凙ES绉橀挜涓嶈兘涓虹┖:"+field.getName());
+                            }
+                        }
+
+                        hasAnnotation = field.isAnnotationPresent(Column.class);
+                        if(hasAnnotation){
+                            Column column = field.getAnnotation(Column.class);
+                            fildName = column.name();
+                        }
+                        if(StringUtils.isEmpty(fildName)){
+                            fildName = field.getName();
+                        }
+
+                        sqlSentence.sqlSentence("SELECT id,"+fildName+" FROM "+tableName,values);
+                        List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
+                        for(Map<String,Object> map:list){
+                            fildValue = (String)map.get(fildName);
+                            System.out.println("fildValue:"+fildValue);
+                            if(StringUtils.isEmpty(fildValue)){
+                                continue;
+                            }
+                            if(fildValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(fildValue)){
+                                continue;
+                            }
+                            values.clear();
+                            values.put("id",map.get("id"));
+                            values.put("filedData",fildValue);
+                            sqlSentence.sqlSentence("UPDATE "+tableName+" SET "+fildName+" = #{m.filedData} WHERE id = #{m.id}",values);
+                            if(commonService.updateSentence(sqlSentence)!=1){
+                                throw new ServiceException("鏇存柊瓒呰繃1鏉★紝鏇存柊澶辫触锛�");
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+
+}

--
Gitblit v1.8.0