diff --git a/framework/src/main/java/cn/lili/common/utils/RegularUtil.java b/framework/src/main/java/cn/lili/common/utils/RegularUtil.java index 3837cf24..3bbf9dba 100644 --- a/framework/src/main/java/cn/lili/common/utils/RegularUtil.java +++ b/framework/src/main/java/cn/lili/common/utils/RegularUtil.java @@ -5,6 +5,7 @@ import java.util.regex.Pattern; /** * 用户名验证工具类 + * * @author Chopper */ public class RegularUtil { @@ -20,21 +21,88 @@ public class RegularUtil { */ private static final Pattern EMAIL = Pattern.compile("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$"); - public static boolean mobile(String v){ + //sql正则 + + static Pattern sqlPattern = Pattern.compile("(select|update|and|delete|insert|trancate|char|substr|ascii|declare|exec|count|master|into|drop|execute" + +// 可能涉及英文查询参数问题 +// "|in|not in exists|not exists" + +// "|between|not between" + +// "|like|not like" + +// "|is null|is not null" + + ")", Pattern.CASE_INSENSITIVE); + + //符号正则 + static Pattern symbolPattern = Pattern.compile("[\\s~·`!!@#¥$%^……&*(())\\-——\\-_=+【\\[\\]】{{}}\\|、\\\\;;::‘'“”\",,《<。.》>、/??]"); + + + /** + * 校验手机号 + * + * @param v + * @return + */ + public static boolean mobile(String v) { Matcher m = MOBILE.matcher(v); - if(m.matches()){ + if (m.matches()) { return true; } return false; } - public static boolean email(String v){ + //校验邮箱 + public static boolean email(String v) { Matcher m = EMAIL.matcher(v); - if(m.matches()){ + if (m.matches()) { return true; } return false; } + + + /** + * 搜索参数过滤 + * + * @param str 字符串 + * @return 过滤后的字符串 + */ + public static String replace(String str) { + + return symbolReplace(sqlReplace(str)); + } + + /** + * 过滤sql关键字 + * + * @param str 字符串 + * @return 过滤后的字符串 + */ + public static String sqlReplace(String str) { + if (StringUtils.isEmpty(str)) { + return ""; + } + Matcher sqlMatcher = sqlPattern.matcher(str); + return sqlMatcher.replaceAll(""); + } + + /** + * 符号过滤 + * + * @param str 字符串 + * @return 过滤后的字符串 + */ + public static String symbolReplace(String str) { + if (StringUtils.isEmpty(str)) { + return ""; + } + Matcher symbolMatcher = symbolPattern.matcher(str); + return symbolMatcher.replaceAll(""); + } + + public static void main(String[] args) { + System.out.println(replace("selectSELECTINORNOTIN123阿松大asdfa!@#$%^&&*()_+{}[]!?>?").trim()); + } + + } diff --git a/framework/src/main/java/cn/lili/modules/search/entity/dto/EsGoodsSearchDTO.java b/framework/src/main/java/cn/lili/modules/search/entity/dto/EsGoodsSearchDTO.java index 2f0759d6..305fd5f6 100644 --- a/framework/src/main/java/cn/lili/modules/search/entity/dto/EsGoodsSearchDTO.java +++ b/framework/src/main/java/cn/lili/modules/search/entity/dto/EsGoodsSearchDTO.java @@ -1,5 +1,7 @@ package cn.lili.modules.search.entity.dto; +import cn.lili.common.utils.RegularUtil; +import cn.lili.common.utils.StringUtils; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -47,4 +49,11 @@ public class EsGoodsSearchDTO { @ApiModelProperty("当前商品skuId,根据当前浏览的商品信息来给用户推荐可能喜欢的商品") private String currentGoodsId; + //过滤搜索关键字 + public String getKeyword() { + if (StringUtils.isNotEmpty(keyword)) { + RegularUtil.replace(this.keyword); + } + return keyword; + } }