Просмотр исходного кода

SQL工具类新增检查关键字方法

RuoYi лет назад: 4
Родитель
Сommit
054fd7546f

+ 24 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/sql/SqlUtil.java

@@ -11,6 +11,11 @@ import com.ruoyi.common.core.utils.StringUtils;
11 11
 public class SqlUtil
12 12
 {
13 13
     /**
14
+     * 定义常用的 sql关键字
15
+     */
16
+    public static String SQL_REGEX = "select |insert |delete |update |drop |count |exec |chr |mid |master |truncate |char |and |declare ";
17
+
18
+    /**
14 19
      * 仅支持字母、数字、下划线、空格、逗号、小数点(支持多个字段排序)
15 20
      */
16 21
     public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+";
@@ -34,4 +39,23 @@ public class SqlUtil
34 39
     {
35 40
         return value.matches(SQL_PATTERN);
36 41
     }
42
+
43
+    /**
44
+     * SQL关键字检查
45
+     */
46
+    public static void filterKeyword(String value)
47
+    {
48
+        if (StringUtils.isEmpty(value))
49
+        {
50
+            return;
51
+        }
52
+        String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|");
53
+        for (int i = 0; i < sqlKeywords.length; i++)
54
+        {
55
+            if (StringUtils.indexOfIgnoreCase(value, sqlKeywords[i]) > -1)
56
+            {
57
+                throw new UtilException("参数存在SQL注入风险");
58
+            }
59
+        }
60
+    }
37 61
 }