From c64e1248bfda3ac8c5120e529fd096dfc4846629 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期四, 13 一月 2022 15:20:24 +0800
Subject: [PATCH] AES加密插件

---
 src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java |  126 +++++++++++++++++++++++++++---------------
 1 files changed, 81 insertions(+), 45 deletions(-)

diff --git a/src/main/java/com/hx/springbean/VariableAesKey.java b/src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java
similarity index 60%
rename from src/main/java/com/hx/springbean/VariableAesKey.java
rename to src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java
index f9168c0..835cb5e 100644
--- a/src/main/java/com/hx/springbean/VariableAesKey.java
+++ b/src/main/java/com/hx/mybatis/aes/springbean/VariableAesKey.java
@@ -1,8 +1,8 @@
-package com.hx.springbean;
+package com.hx.mybatis.aes.springbean;
 
+import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
 import com.hx.common.annotations.MysqlHexAes;
 import com.hx.util.StringUtils;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
@@ -24,8 +24,16 @@
     @Resource
     private ConstantBean constantBean;
 
-    /**瀛樺偍AES鐨勭閽�*/
+    /**鏄惁宸茬粡鍚姩瀹�*/
+    public static int isRun = 0;
+
+    /**瀛樺偍鎵�鏈堿ES鐨勭閽�*/
     public static Map<String,String> aesKeys = new HashMap<>();
+    /**鏍规嵁琛ㄦ槑鏉ュ瓨鍌ˋES绉橀挜*/
+    public static Map<String,Map<String,String>> aesKeysTable = new HashMap<>();
+
+    /**鍥哄畾鐨刟es绉橀挜*/
+    public static String AES_KEY = null;
 
     /**瀛樺偍AES绉橀挜*/
     public static void setAesKey(String aesKeyFild,String aesKey){
@@ -33,7 +41,14 @@
     }
     /**鑾峰彇AES绉橀挜*/
     public static String getAesKey(String aesKeyFild){
-        return aesKeys.get(aesKeyFild);
+        if(aesKeyFild == null){
+            return AES_KEY;
+        }
+        if(StringUtils.isEmpty(aesKeys.get(aesKeyFild))){
+            return AES_KEY;
+        }else {
+            return  aesKeys.get(aesKeyFild);
+        }
     }
 
     /**
@@ -41,11 +56,29 @@
      */
     @PostConstruct
     public void VariableAesKey(){
+
+        isRun = 1;
         //椤圭洰鍚姩鐨勬椂鍊欏~鍏�
         System.err.println("鎵弿鑾峰彇AES:" + constantBean.getPackPath());
+        AES_KEY = constantBean.getFixedAesKey();
         if(StringUtils.noNull(constantBean.getPackPath())){
             Set<Class<?>> classes = classData(constantBean.getPackPath());
+
+            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){
+                    continue;
+                }
+                tableName = table.name();
+
+                aesKeysFild = new HashMap<>();
+                isAes = false;
+
                 // 鍙栧緱鏈被鐨勫叏閮ㄥ睘鎬�
                 Field[] fields = cl.getDeclaredFields();
                 fields = getPatentFields(fields,cl);
@@ -56,32 +89,31 @@
                         // 鏍规嵁娉ㄨВ绫诲瀷杩斿洖鏂规硶鐨勬寚瀹氱被鍨嬫敞瑙�
                         MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
 
-                        String aesKeyField = mysqlHexAes.aesKeyField();
+                        //String aesKeyField = mysqlHexAes.aesKeyField();
                         String aesKey = mysqlHexAes.aesKey();
 
                         if(StringUtils.isEmpty(aesKey)){
-                            throw new RuntimeException("mysql鐨凙ES绉橀挜涓嶈兘涓虹┖:"+field.getName());
-                        }
-                        if(StringUtils.noNull(aesKeyField)){
-                            String key = aesKeys.get(aesKeyField);
-                            if(StringUtils.isEmpty(key)){
-                                aesKeys.put(aesKeyField,aesKey);
-                            }else{
-                                if(!aesKey.equals(key)){
-                                    throw new RuntimeException("瀛楁/瀹氫箟鐨凙ES绉橀挜瀛楁銆�"+field.getName()+"銆戝涓竴鏍凤紝浣嗘槸AES绉橀挜涓嶄竴鏍�");
-                                }
+                            aesKey = constantBean.getFixedAesKey();
+                            if(StringUtils.isEmpty(aesKey)){
+                                throw new RuntimeException("mysql鐨凙ES绉橀挜涓嶈兘涓虹┖:"+field.getName());
                             }
+                        }
+                        String key = aesKeys.get(field.getName());
+                        if(StringUtils.isEmpty(key)){
+                            aesKeys.put(field.getName(),aesKey);
+                            aesKeysFild.put(field.getName(),aesKey);
+                            isAes = true;
                         }else{
-                            String key = aesKeys.get(field.getName());
-                            if(StringUtils.isEmpty(key)){
-                                aesKeys.put(field.getName(),aesKey);
-                            }else{
-                                if(!aesKey.equals(key)){
-                                    throw new RuntimeException("瀛楁/瀹氫箟鐨凙ES绉橀挜瀛楁銆�"+field.getName()+"銆戝涓竴鏍凤紝浣嗘槸AES绉橀挜涓嶄竴鏍�");
-                                }
+                            isAes = true;
+                            aesKeysFild.put(field.getName(),aesKey);
+                            if(!aesKey.equals(key)){
+                                throw new RuntimeException("瀛楁/瀹氫箟鐨凙ES绉橀挜瀛楁銆�"+field.getName()+"銆戝涓竴鏍凤紝浣嗘槸AES绉橀挜涓嶄竴鏍�");
                             }
                         }
                     }
+                }
+                if(isAes){
+                    aesKeysTable.put(tableName,aesKeysFild);
                 }
             }
         }
@@ -90,31 +122,35 @@
     /**鑾峰彇鍖呬笅闈㈢殑鎵�鏈夋枃浠�*/
     public static Set<Class<?>> classData(String packPath){
         Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
-        // 鏄惁寰幆杩唬
-        boolean recursive = true;
-        // 鑾峰彇鍖呯殑鍚嶅瓧 骞惰繘琛屾浛鎹�
-        String packageName = packPath;
-        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[] packPaths = packPath.split(";|,");
+        for( String packageName : packPaths){
+            // 鏄惁寰幆杩唬
+            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);
+                    }
                 }
+            }catch (IOException e){
+                e.printStackTrace();
             }
-        }catch (IOException e){
-            e.printStackTrace();
         }
         return classes;
     }

--
Gitblit v1.8.0