From 054fd7546f90c7875b6477f807c19f49dc0eb40c Mon Sep 17 00:00:00 2001 From: RuoYi Date: Tue, 21 Dec 2021 13:41:40 +0800 Subject: [PATCH] =?UTF-8?q?SQL=E5=B7=A5=E5=85=B7=E7=B1=BB=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=A3=80=E6=9F=A5=E5=85=B3=E9=94=AE=E5=AD=97=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/core/utils/sql/SqlUtil.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/sql/SqlUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/sql/SqlUtil.java index 00a18ef..271b58d 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/sql/SqlUtil.java +++ b/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; public class SqlUtil { /** + * 定义常用的 sql关键字 + */ + public static String SQL_REGEX = "select |insert |delete |update |drop |count |exec |chr |mid |master |truncate |char |and |declare "; + + /** * 仅支持字母、数字、下划线、空格、逗号、小数点(支持多个字段排序) */ public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+"; @@ -34,4 +39,23 @@ public class SqlUtil { return value.matches(SQL_PATTERN); } + + /** + * SQL关键字检查 + */ + public static void filterKeyword(String value) + { + if (StringUtils.isEmpty(value)) + { + return; + } + String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|"); + for (int i = 0; i < sqlKeywords.length; i++) + { + if (StringUtils.indexOfIgnoreCase(value, sqlKeywords[i]) > -1) + { + throw new UtilException("参数存在SQL注入风险"); + } + } + } }