chenjiahe
2022-01-28 f36821f2611e7bac0692572c02e474aec6d258fb
Mysql数据库AES加密工具
2个文件已修改
112 ■■■■■ 已修改文件
src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java 106 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java
@@ -11,13 +11,13 @@
public class ConstantBean {
    /**获取AES秘钥的配置(从什么包获取到)*/
    @Value("${mysql.hxe.aes.find.packs:null}")
    @Value("${mysql.hxe.aes.find.packs:}")
    private String packPath;
    /**固定AES的秘钥*/
    @Value("${mysql.hxe.aes.fixd.key:null}")
    @Value("${mysql.hxe.aes.fixd.key:}")
    private String fixedAesKey;
    /**数据库初始化加密字段版本号*/
    @Value("${mysql.hxe.aes.init.version:null}")
    @Value("${mysql.hxe.aes.init.version:}")
    private String initVersion;
    public String getPackPath() {
src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
@@ -13,6 +13,8 @@
import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class InitMysqlData {
@@ -32,50 +34,80 @@
            //解析表数据
            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);
                }
                sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName(),values);
                //更新数据
                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+"}");
            int pageSize = 500;
            //创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(20);
            try{
                for(FieldData fieldData:fieldDatas){
                    //获取条数
                    sqlSentence.sqlSentence("SELECT COUNT(0) FROM "+fieldData.getTableName(),values);
                    int total = commonService.selectCountSql(sqlSentence);
                    if(total ==0 ){
                        continue;
                    }
                    values = map;
                    sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE id = #{m.id}",values);
                    if(commonService.updateSentence(sqlSentence)!=1){
                        throw new ServiceException("更新超过1条,更新失败!");
                    int pageTotal = total/pageSize+1;
                    for(int i= 0;i<pageTotal;i++){
                        int finalI = i;
                        fixedThreadPool.execute(new Runnable() {
                            @Override
                            public void run() {
                                //更新数据
                                updateData(fieldData,commonService, finalI,pageSize);
                            }
                        });
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                fixedThreadPool.shutdown();
            }
        }
    }
    /**更新数据*/
    public static void updateData(FieldData fieldData,CommonService commonService,int pageNum,int pageSize){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        //查询数据
        StringBuilder selectField = new StringBuilder();
        selectField.append(fieldData.getId());
        for(String fieldName:fieldData.getEncrypFields()){
            selectField.append(","+fieldName);
        }
        sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName()+" LIMIT "+pageNum+","+pageSize,values);
        List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
        for (Map<String,Object> map:list){
            StringBuilder 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(fieldData.getId().equals(mapKey)){
                    continue;
                }
                if(setField.length()>0){
                    setField.append(",");
                }
                setField.append(mapKey+" = #{m."+mapKey+"}");
            }
            values = map;
            sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE "+fieldData.getId()+" = #{m."+fieldData.getId()+"}",values);
            if(commonService.updateSentence(sqlSentence)!=1){
                throw new ServiceException("更新超过1条,更新失败!");
            }
        }
    }
    /**获取到表的数据*/
    public static List<FieldData> entityhandle(Set<Class<?>> classes){