From 0344dc66783353faff118a0ea91f7d2aa07bbd4a Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期六, 02 四月 2022 18:47:44 +0800
Subject: [PATCH] 优化加密初始化方法

---
 src/main/java/com/hx/mybatis/aes/springbean/SqlUtils.java |   42 +++++++++++++++++++++++++++---------------
 1 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/hx/mybatis/aes/springbean/SqlUtils.java b/src/main/java/com/hx/mybatis/aes/springbean/SqlUtils.java
index 9281129..fe8e437 100644
--- a/src/main/java/com/hx/mybatis/aes/springbean/SqlUtils.java
+++ b/src/main/java/com/hx/mybatis/aes/springbean/SqlUtils.java
@@ -2,6 +2,7 @@
 
 import com.alibaba.druid.sql.SQLUtils;
 import com.alibaba.druid.sql.ast.SQLExpr;
+import com.alibaba.druid.sql.ast.SQLObject;
 import com.alibaba.druid.sql.ast.SQLStatement;
 import com.alibaba.druid.sql.ast.statement.*;
 import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
@@ -15,6 +16,7 @@
 import com.alibaba.druid.util.JdbcUtils;
 import com.hx.util.StringUtils;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -66,20 +68,22 @@
             if(sqlSelect.length() > 7){
                 sqlSelect.append(",");
             }
-            expr = sqlSelectItem.getExpr().toString();
-            if(expr.indexOf("SELECT") == -1){
+
+            out.delete(0, out.length()) ;
+            sqlSelectItem.accept(sqlastOutputVisitor) ;
+            expr = out.toString();
+            sqlSelect.append(expr);
+
+           /* if(expr.indexOf("SELECT") == -1){
                 sqlSelect.append(expr);
-                if(!StringUtils.isEmpty(sqlSelectItem.getAlias())){
-                    sqlSelect.append(" AS "+sqlSelectItem.getAlias());
-                }
             }else{
-                sqlSelect.append("(");
-                sqlSelect.append(selectSqlHandle(expr,aesKeysTable,tableMaps,columns));
-                sqlSelect.append(")");
-                if(!StringUtils.isEmpty(sqlSelectItem.getAlias())){
+                //sqlSelect.append("(");
+                sqlSelect.append(expr);
+                //sqlSelect.append(")");
+               *//* if(!StringUtils.isEmpty(sqlSelectItem.getAlias())){
                     sqlSelect.append(" AS "+sqlSelectItem.getAlias());
-                }
-            }
+                }*//*
+            }*/
         }
 
         //瑙f瀽from
@@ -134,12 +138,20 @@
                 }else{
                     tableAl = column.getName();
                 }
-                sql = sql.replaceAll("( |\\n|\\()"+tableAl+"( |\\n|\\))"," AES_DECRYPT(UNHEX("+tableAl+"),'"+aeskey+"') ");
+                sql = sql.replaceAll("((?<!\\.)\\b"+tableAl+"\\b(?!\\.))","AES_DECRYPT(UNHEX("+tableAl+"),'"+aeskey+"')");
             }
         }
         return sqlSelect.toString()+sql;
     }
 
+    /**
+     * 澶勭悊select杩斿洖瀛楁鐨勫弬鏁�
+     * @param sql
+     * @param aesKeysTable
+     * @param tableMaps
+     * @param columns
+     * @return
+     */
     public static String selectSqlHandle(String sql,Map<String,Map<String,String>> aesKeysTable
             ,Map<String,String> tableMaps,Collection<TableStat.Column> columns){
 
@@ -235,7 +247,7 @@
                 }else{
                     tableAl = column.getName();
                 }
-                sql = sql.replaceAll("( |\\n|\\()"+tableAl+"( |\\n|\\))"," AES_DECRYPT(UNHEX("+tableAl+"),'"+aeskey+"') ");
+                sql = sql.replaceAll("((?<!\\.)\\b"+tableAl+"\\b(?!\\.))","AES_DECRYPT(UNHEX("+tableAl+"),'"+aeskey+"')");
             }
         }
         return sqlSelect.toString()+sql;
@@ -399,7 +411,7 @@
                 }else{
                     tableAl = column.getName();
                 }
-                sqlWhere = sqlWhere.replaceAll("( |\\n|\\()"+tableAl+"( |\\n|\\))"," AES_DECRYPT(UNHEX("+tableAl+"),'"+aeskey+"') ");
+                sqlWhere = sqlWhere.replaceAll("((?<!\\.)\\b"+tableAl+"\\b(?!\\.))","AES_DECRYPT(UNHEX("+tableAl+"),'"+aeskey+"')");
             }
 
         }
@@ -477,7 +489,7 @@
                 }else{
                     tableAl = column.getName();
                 }
-                sqlWhere = sqlWhere.replaceAll("( |\\n|\\()"+tableAl+"( |\\n|\\))"," AES_DECRYPT(UNHEX("+tableAl+"),'"+aeskey+"') ");
+                sqlWhere = sqlWhere.replaceAll("((?<!\\.)\\b"+tableAl+"\\b(?!\\.))","AES_DECRYPT(UNHEX("+tableAl+"),'"+aeskey+"')");
             }
 
         }

--
Gitblit v1.8.0