From 346eb22b5ab622064d1f61819240656529800f2b Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期五, 14 一月 2022 13:05:53 +0800 Subject: [PATCH] Mysql数据库AES加密工具 --- src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java | 119 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 88 insertions(+), 31 deletions(-) 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 835cb5e..b9dba4c 100644 --- a/src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java +++ b/src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java @@ -3,6 +3,8 @@ import com.gitee.sunchenbin.mybatis.actable.annotation.Table; import com.hx.common.annotations.MysqlHexAes; import com.hx.util.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @@ -11,15 +13,21 @@ import java.io.FileFilter; import java.io.IOException; import java.lang.reflect.Field; +import java.net.JarURLConnection; import java.net.URL; import java.net.URLDecoder; import java.util.*; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; /** * 鑾峰彇鎸囧畾鍖呴噷闈㈢殑AES绉橀挜 */ @Component public class VariableAesKey { + + //log4j鏃ュ織 + private static Logger logger = LoggerFactory.getLogger(VariableAesKey.class.getName()); @Resource private ConstantBean constantBean; @@ -59,21 +67,22 @@ isRun = 1; //椤圭洰鍚姩鐨勬椂鍊欏~鍏� - System.err.println("鎵弿鑾峰彇AES:" + constantBean.getPackPath()); + logger.info("鎵弿鑾峰彇AES鐨勫寘:" + constantBean.getPackPath()); AES_KEY = constantBean.getFixedAesKey(); - if(StringUtils.noNull(constantBean.getPackPath())){ + if(!StringUtils.isEmpty(constantBean.getPackPath())){ Set<Class<?>> classes = classData(constantBean.getPackPath()); - + logger.info("鎵弿鑾峰彇AES鐨勫寘classes:" + classes.size()); Map<String,String> aesKeysFild = new HashMap<>(); boolean isAes = false; String tableName = null; for(Class<?> cl:classes){ //琛ㄥ悕绉� - Table table = cl.getAnnotation(Table.class); - if(table == null){ + boolean hasAnnotation = cl.isAnnotationPresent(Table.class); + if(!hasAnnotation){ continue; } + Table table = cl.getAnnotation(Table.class); tableName = table.name(); aesKeysFild = new HashMap<>(); @@ -84,7 +93,7 @@ fields = getPatentFields(fields,cl); for (Field field:fields) { // 鍒ゆ柇鏂规硶涓槸鍚︽湁鎸囧畾娉ㄨВ绫诲瀷鐨勬敞瑙� - boolean hasAnnotation = field.isAnnotationPresent(MysqlHexAes.class); + hasAnnotation = field.isAnnotationPresent(MysqlHexAes.class); if (hasAnnotation) { // 鏍规嵁娉ㄨВ绫诲瀷杩斿洖鏂规硶鐨勬寚瀹氱被鍨嬫敞瑙� MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class); @@ -121,37 +130,85 @@ /**鑾峰彇鍖呬笅闈㈢殑鎵�鏈夋枃浠�*/ public static Set<Class<?>> classData(String packPath){ - Set<Class<?>> classes = new LinkedHashSet<Class<?>>(); + Set<Class<?>> classes = new LinkedHashSet(); + String[] split = packPath.split(",|;"); + String[] var3 = split; + int var4 = split.length; - //鎴彇 - String[] packPaths = packPath.split(";|,"); - for( String packageName : packPaths){ - // 鏄惁寰幆杩唬 + label82: + for(int var5 = 0; var5 < var4; ++var5) { + String pack = var3[var5]; boolean recursive = true; - // 鑾峰彇鍖呯殑鍚嶅瓧 骞惰繘琛屾浛鎹� - String packageDirName = packageName.replace('.', '/'); - // 瀹氫箟涓�涓灇涓剧殑闆嗗悎 骞惰繘琛屽惊鐜潵澶勭悊杩欎釜鐩綍涓嬬殑things - Enumeration<URL> dirs; - try{ - dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName); - // 寰幆杩唬涓嬪幓 - while (dirs.hasMoreElements()){ - // 鑾峰彇涓嬩竴涓厓绱� - URL url = dirs.nextElement(); - // 寰楀埌鍗忚鐨勫悕绉� - String protocol = url.getProtocol(); - // 濡傛灉鏄互鏂囦欢鐨勫舰寮忎繚瀛樺湪鏈嶅姟鍣ㄤ笂 - if ("file".equals(protocol)) { - // 鑾峰彇鍖呯殑鐗╃悊璺緞 - String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); - // 浠ユ枃浠剁殑鏂瑰紡鎵弿鏁翠釜鍖呬笅鐨勬枃浠� 骞舵坊鍔犲埌闆嗗悎涓� - findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes); + String packageName = pack; + String packageDirName = pack.replace('.', '/'); + + try { + Enumeration dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName); + + while(true) { + label75: + while(true) { + if (!dirs.hasMoreElements()) { + continue label82; + } + + URL url = (URL)dirs.nextElement(); + String protocol = url.getProtocol(); + if ("file".equals(protocol)) { + System.err.println("file绫诲瀷鐨勬壂鎻�:" + pack); + String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); + findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes); + } else if ("jar".equals(protocol)) { + System.err.println("jar绫诲瀷鐨勬壂鎻�"); + + try { + JarFile jar = ((JarURLConnection)url.openConnection()).getJarFile(); + Enumeration entries = jar.entries(); + + while(true) { + JarEntry entry; + String name; + int idx; + do { + do { + if (!entries.hasMoreElements()) { + continue label75; + } + + entry = (JarEntry)entries.nextElement(); + name = entry.getName(); + if (name.charAt(0) == '/') { + name = name.substring(1); + } + } while(!name.startsWith(packageDirName)); + + idx = name.lastIndexOf(47); + if (idx != -1) { + packageName = name.substring(0, idx).replace('/', '.'); + } + } while(idx == -1 && !recursive); + + if (name.endsWith(".class") && !entry.isDirectory()) { + String className = name.substring(packageName.length() + 1, name.length() - 6); + + try { + classes.add(Class.forName(packageName + '.' + className)); + } catch (ClassNotFoundException var20) { + var20.printStackTrace(); + } + } + } + } catch (IOException var21) { + var21.printStackTrace(); + } + } } } - }catch (IOException e){ - e.printStackTrace(); + } catch (IOException var22) { + var22.printStackTrace(); } } + return classes; } -- Gitblit v1.8.0