作者 RuoYi

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

@@ -11,6 +11,11 @@ import com.ruoyi.common.utils.StringUtils; @@ -11,6 +11,11 @@ import com.ruoyi.common.utils.StringUtils;
11 public class SqlUtil 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 public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+"; 21 public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+";
@@ -34,4 +39,23 @@ public class SqlUtil @@ -34,4 +39,23 @@ public class SqlUtil
34 { 39 {
35 return value.matches(SQL_PATTERN); 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 }