From b1097dce55d857ae1ae63798911c1e7027c00de4 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期五, 28 一月 2022 14:55:11 +0800 Subject: [PATCH] Mysql数据库AES加密工具 --- src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java | 11 ++ src/main/java/com/hx/common/xml/CommonMapper.xml | 5 + src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java | 182 +++++++++++++++++++++++------------- src/main/java/com/hx/common/service/impl/CommonServiceImpl.java | 6 + src/main/java/com/hx/common/service/impl/CommonDaoImpl.java | 5 + src/main/java/com/hx/mybatis/aes/springbean/FieldData.java | 40 ++++++++ src/main/java/com/hx/platform/tool/PlatformSign.java | 0 src/main/java/com/hx/common/annotations/MysqlHexAes.java | 2 src/main/java/com/hx/common/dao/CommonMapper.java | 3 src/main/java/com/hx/common/service/CommonService.java | 5 + src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java | 5 + src/main/java/com/hx/common/dao/CommonDao.java | 6 + 12 files changed, 204 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/hx/common/annotations/MysqlHexAes.java b/src/main/java/com/hx/common/annotations/MysqlHexAes.java index 4696b88..8e5d753 100644 --- a/src/main/java/com/hx/common/annotations/MysqlHexAes.java +++ b/src/main/java/com/hx/common/annotations/MysqlHexAes.java @@ -12,6 +12,8 @@ public @interface MysqlHexAes { /**绉橀挜-娌℃湁灏辨槸閰嶇疆鏂囦欢璁剧疆*/ String aesKey() default ""; + /**鏁版嵁搴撴暟鎹垵濮嬪寲鐗堟湰*/ + String initVersion() default ""; /**xml鐢熸垚鏌ヨ瑙e瘑*/ boolean selectDec() default false; /**xml鏇存柊鍔犲瘑*/ diff --git a/src/main/java/com/hx/common/dao/CommonDao.java b/src/main/java/com/hx/common/dao/CommonDao.java index 3a5c32d..ddc43e8 100644 --- a/src/main/java/com/hx/common/dao/CommonDao.java +++ b/src/main/java/com/hx/common/dao/CommonDao.java @@ -110,5 +110,11 @@ */ <T extends Serializable> int updateSentence( SqlSentence sqlSentence); + /**鏌ヨ鏉℃暟璇彞锛堝叏璇彞锛� + * @param sqlSentence 鏌ヨ鍙傛暟绫� + * @return 杩斿洖鏉℃暟 + */ + <T extends Serializable> int selectCountSql( 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 index 4b29de2..4f6d5d0 100644 --- a/src/main/java/com/hx/common/dao/CommonMapper.java +++ b/src/main/java/com/hx/common/dao/CommonMapper.java @@ -14,4 +14,7 @@ /**鏌ヨ鍒楄〃锛岃繑鍥濵ap鐨凩ist*/ List<Map<String,Object>> selectListMap(SqlSentence sqlSentence); + /**鏌ヨ锛岃繑鍥炴潯鏁版暟閲�*/ + int selectCountSql(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 312786c..d2b0642 100644 --- a/src/main/java/com/hx/common/service/CommonService.java +++ b/src/main/java/com/hx/common/service/CommonService.java @@ -111,5 +111,10 @@ */ <T extends Serializable> int updateSentence(SqlSentence sqlSentence); + /**鏌ヨ鏉℃暟璇彞锛堝叏璇彞锛� + * @param sqlSentence 鏌ヨ鍙傛暟绫� + * @return 杩斿洖鏉℃暟 + */ + <T extends Serializable> int selectCountSql(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 685e468..3dd14f8 100644 --- a/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java +++ b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java @@ -109,5 +109,10 @@ return sqlSessionFactory.openSession().delete(getStatement(CommonMapper.class,"updateSentence"),sqlSentence); } + /**鏌ヨ鏉℃暟锛堝叏璇彞锛�*/ + @Override + public <T extends Serializable> int selectCountSql(SqlSentence sqlSentence) { + return sqlSessionFactory.openSession().delete(getStatement(CommonMapper.class,"selectCountSql"),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 078f8d2..ec5b0f4 100644 --- a/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java +++ b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java @@ -104,5 +104,11 @@ return commonDao.updateSentence(sqlSentence); } + /**鏇存柊sql璇彞锛堝叏璇彞锛�*/ + @Override + public <T extends Serializable> int selectCountSql(SqlSentence sqlSentence) { + return commonDao.selectCountSql(sqlSentence); + } + } diff --git a/src/main/java/com/hx/common/xml/CommonMapper.xml b/src/main/java/com/hx/common/xml/CommonMapper.xml index 968338a..9fbc165 100644 --- a/src/main/java/com/hx/common/xml/CommonMapper.xml +++ b/src/main/java/com/hx/common/xml/CommonMapper.xml @@ -11,4 +11,9 @@ <select id="selectListMap" resultType="java.util.Map" parameterType="com.hx.mybatisTool.SqlSentence" > ${sqlSentence} </select> + + <select id="selectCountSql" resultType="int" 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/ConstantBean.java b/src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java index d64a839..f7281c2 100644 --- a/src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java +++ b/src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java @@ -16,6 +16,9 @@ /**鍥哄畾AES鐨勭閽�*/ @Value("${mysql.hxe.aes.fixd.key:null}") private String fixedAesKey; + /**鏁版嵁搴撳垵濮嬪寲鍔犲瘑瀛楁鐗堟湰鍙�*/ + @Value("${mysql.hxe.aes.init.version:null}") + private String initVersion; public String getPackPath() { return packPath; @@ -32,4 +35,12 @@ public void setFixedAesKey(String fixedAesKey) { this.fixedAesKey = fixedAesKey; } + + public String getInitVersion() { + return initVersion; + } + + public void setInitVersion(String initVersion) { + this.initVersion = initVersion; + } } diff --git a/src/main/java/com/hx/mybatis/aes/springbean/FieldData.java b/src/main/java/com/hx/mybatis/aes/springbean/FieldData.java new file mode 100644 index 0000000..cd54cbc --- /dev/null +++ b/src/main/java/com/hx/mybatis/aes/springbean/FieldData.java @@ -0,0 +1,40 @@ +package com.hx.mybatis.aes.springbean; + +import java.util.Set; + +public class FieldData { + + //涓婚敭 + private String id; + //鏁版嵁搴撹〃鏄� + private String tableName; + //闇�瑕佸姞瀵嗗緱琛ㄥ瓧娈� + private Set<String> encrypFields; + + public FieldData() { + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTableName() { + return tableName; + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + public Set<String> getEncrypFields() { + return encrypFields; + } + + public void setEncrypFields(Set<String> encrypFields) { + this.encrypFields = encrypFields; + } +} diff --git a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java index 2c5abcc..92b46d7 100644 --- a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java +++ b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java @@ -12,10 +12,7 @@ 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; +import java.util.*; public class InitMysqlData { @@ -28,81 +25,134 @@ //椤圭洰鍚姩鐨勬椂鍊欏~鍏� 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; + //瑙f瀽琛ㄦ暟鎹� + List<FieldData> fieldDatas = entityhandle(classes); + + StringBuilder selectField; + StringBuilder setField; + for(FieldData fieldData:fieldDatas){ + //鑾峰彇鏉℃暟 + sqlSentence.sqlSentence("SELECT COUNT(0) FROM "+fieldData.getTableName(),values); + int total = commonService.selectCountSql(sqlSentence); + + //鏌ヨ鏁版嵁 + selectField = new StringBuilder(); + selectField.append(fieldData.getId()); + for(String fieldName:fieldData.getEncrypFields()){ + selectField.append(","+fieldName); } - Table table = cl.getAnnotation(Table.class); - tableName = table.name(); + sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName(),values); - 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()); - } + //鏇存柊鏁版嵁 + List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence); + for (Map<String,Object> map:list){ + setField = new StringBuilder(); + for (Map.Entry<String, Object> entry : map.entrySet()) { + String mapKey = entry.getKey(); + String mapValue = (String) entry.getValue(); + if(StringUtils.isEmpty(mapValue)){ + continue; } + if(mapValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(mapValue)){ + continue; + } + if(setField.length()>0){ + setField.append(","); + } + setField.append(mapKey+" = #{m."+mapKey+"}"); + } - 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鏉★紝鏇存柊澶辫触锛�"); - } - } + values = map; + sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE id = #{m.id}",values); + if(commonService.updateSentence(sqlSentence)!=1){ + throw new ServiceException("鏇存柊瓒呰繃1鏉★紝鏇存柊澶辫触锛�"); } } } } } + /**鑾峰彇鍒拌〃鐨勬暟鎹�*/ + public static List<FieldData> entityhandle(Set<Class<?>> classes){ + + List<FieldData> fildDatas = new ArrayList<>(); + //瀛樺偍鍗曡〃瀛楁淇℃伅 + FieldData fildData; + //瀛樺偍闇�瑕佸姞瀵嗙殑瀛楁 + Set<String> encrypFields; + + String fildName; + + for(Class<?> cl:classes){ + fildData = new FieldData(); + encrypFields = new HashSet<>(); + + //琛ㄥ悕绉� + if(!cl.isAnnotationPresent(Table.class)){ + continue; + } + Table table = cl.getAnnotation(Table.class); + fildData.setTableName(table.name()); + + // 鍙栧緱鏈被鐨勫叏閮ㄥ睘鎬� + Field[] fields = cl.getDeclaredFields(); + fields = VariableAesKey.getPatentFields(fields,cl); + for (Field field:fields) { + + fildName = null; + if(field.isAnnotationPresent(Column.class)){ + Column column = field.getAnnotation(Column.class); + fildName = column.name(); + if(StringUtils.isEmpty(fildName)){ + fildName = field.getName(); + } + if(column.isKey()){ + fildData.setId(fildName); + } + }else{ + fildName = field.getName(); + } + + // 鍒ゆ柇鏂规硶涓槸鍚︽湁鎸囧畾娉ㄨВ绫诲瀷鐨勬敞瑙� + if (!field.isAnnotationPresent(MysqlHexAes.class)) { + continue; + } + // 鏍规嵁娉ㄨВ绫诲瀷杩斿洖鏂规硶鐨勬寚瀹氱被鍨嬫敞瑙� + MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class); + //鍒ゆ柇鐗堟湰鍙锋槸涓嶆槸涓�鏍风殑 + if(!StringUtils.isEmpty(VariableAesKey.INIT_VERSION)){ + if(!VariableAesKey.INIT_VERSION.equals(mysqlHexAes.initVersion())){ + continue; + } + }else{ + if(!StringUtils.isEmpty(mysqlHexAes.initVersion())){ + continue; + } + } + + String aesKey = mysqlHexAes.aesKey(); + if(StringUtils.isEmpty(aesKey)){ + aesKey = VariableAesKey.AES_KEY; + if(StringUtils.isEmpty(aesKey)){ + throw new RuntimeException("mysql鐨凙ES绉橀挜涓嶈兘涓虹┖:"+field.getName()); + } + } + + encrypFields.add(fildName); + } + //鏄惁鏈夐渶瑕佸姞瀵嗗緱瀛楁 + if(encrypFields.size()<=0){ + continue; + } + fildData.setEncrypFields(encrypFields); + fildDatas.add(fildData); + } + return fildDatas; + } + } diff --git a/src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java b/src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java index b9dba4c..e3f8fa2 100644 --- a/src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java +++ b/src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java @@ -42,6 +42,10 @@ /**鍥哄畾鐨刟es绉橀挜*/ public static String AES_KEY = null; + /**鏁版嵁搴撳姞瀵嗗瓧娈靛垵濮嬪寲鐗堟湰鍙�*/ + public static String INIT_VERSION = null; + + /**瀛樺偍AES绉橀挜*/ public static void setAesKey(String aesKeyFild,String aesKey){ @@ -69,6 +73,7 @@ //椤圭洰鍚姩鐨勬椂鍊欏~鍏� logger.info("鎵弿鑾峰彇AES鐨勫寘:" + constantBean.getPackPath()); AES_KEY = constantBean.getFixedAesKey(); + INIT_VERSION = constantBean.getInitVersion(); if(!StringUtils.isEmpty(constantBean.getPackPath())){ Set<Class<?>> classes = classData(constantBean.getPackPath()); logger.info("鎵弿鑾峰彇AES鐨勫寘classes:" + classes.size()); diff --git a/src/main/java/com/hx/platform/tool/PlatformSign.java b/src/main/java/com/hx/platform/tool/PlatformSign.java new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/main/java/com/hx/platform/tool/PlatformSign.java -- Gitblit v1.8.0