'搜索商品功能过滤关键字以及特殊字符'

This commit is contained in:
Chopper711 2022-12-07 15:14:25 +08:00
parent d6393e39f7
commit 602678e2b9
2 changed files with 81 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import java.util.regex.Pattern;
/** /**
* 用户名验证工具类 * 用户名验证工具类
*
* @author Chopper * @author Chopper
*/ */
public class RegularUtil { 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}$"); 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); Matcher m = MOBILE.matcher(v);
if(m.matches()){ if (m.matches()) {
return true; return true;
} }
return false; return false;
} }
public static boolean email(String v){ //校验邮箱
public static boolean email(String v) {
Matcher m = EMAIL.matcher(v); Matcher m = EMAIL.matcher(v);
if(m.matches()){ if (m.matches()) {
return true; return true;
} }
return false; 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());
}
} }

View File

@ -1,5 +1,7 @@
package cn.lili.modules.search.entity.dto; 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 io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@ -47,4 +49,11 @@ public class EsGoodsSearchDTO {
@ApiModelProperty("当前商品skuId,根据当前浏览的商品信息来给用户推荐可能喜欢的商品") @ApiModelProperty("当前商品skuId,根据当前浏览的商品信息来给用户推荐可能喜欢的商品")
private String currentGoodsId; private String currentGoodsId;
//过滤搜索关键字
public String getKeyword() {
if (StringUtils.isNotEmpty(keyword)) {
RegularUtil.replace(this.keyword);
}
return keyword;
}
} }