|
|
@@ -0,0 +1,136 @@
|
|
|
+package com.gis.common.util;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.extra.pinyin.PinyinUtil;
|
|
|
+import com.gis.common.exception.BaseRuntimeException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2021/11/18 0011 16:16
|
|
|
+ * 字符串过滤
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class RegexUtil {
|
|
|
+
|
|
|
+ // 需要过滤的特殊字符
|
|
|
+// String [] specialSql = {"%","or","=","and","truncate","delete","update","exec","'",";"};
|
|
|
+ static List<String> specialSql = Arrays.asList("%","or","=","and","truncate","delete","update","exec","'",";");
|
|
|
+
|
|
|
+ // 特殊符号
|
|
|
+ static List<String> symbol = Arrays.asList("%","=","'",";");
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /** 处理特殊符号,变空值*/
|
|
|
+ public static String specificSymbol(String str){
|
|
|
+
|
|
|
+ String regEx = "[\\s`~!@#$%^&*()+=|{}':;\\[\\]<>/?·~!@#¥%……&*()——+|{}【】‘;:“”。,、?]";
|
|
|
+ return str.replaceAll(regEx, "");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /** 中文转拼音*/
|
|
|
+ public static String getPinyinName(String str){
|
|
|
+ // 去除特殊符号
|
|
|
+ String pinyinName = RegexUtil.specificSymbol(str);
|
|
|
+ pinyinName = PinyinUtil.getPinyin(pinyinName, "");
|
|
|
+ // 转小写
|
|
|
+ pinyinName = pinyinName.toLowerCase();
|
|
|
+ return pinyinName;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**防止sql注入*/
|
|
|
+ public static void regSql(String str){
|
|
|
+ String key = "and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+";
|
|
|
+ String[] split = StrUtil.split(key, "|");
|
|
|
+ List<String> list = Arrays.asList(split);
|
|
|
+ for (String s : list) {
|
|
|
+ if (str.toLowerCase().contains(s)){
|
|
|
+ String msg = "存在sql注入字符";
|
|
|
+ log.error(msg);
|
|
|
+ throw new BaseRuntimeException(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * sql 过滤特殊字符
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String sqlReplaceSpecialStr(String str){
|
|
|
+ str = StrUtil.trim(str);
|
|
|
+ str = str.toLowerCase();
|
|
|
+// for (String s : specialSql) {
|
|
|
+// if (str.contains(s)) {
|
|
|
+// str = str.replaceAll(s, "");
|
|
|
+// }
|
|
|
+// }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String cutSpecial(String str){
|
|
|
+ log.info("input:{}", str);
|
|
|
+ String[] split = str.split("\\s+");
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ boolean flag = false;
|
|
|
+ int i = 0;
|
|
|
+ for (String s : split) {
|
|
|
+ if (i > 0){
|
|
|
+ builder.append(" ");
|
|
|
+ }
|
|
|
+ for (String sym : symbol) {
|
|
|
+ if (s.contains(sym)){
|
|
|
+ log.warn("出现了特殊符号; input:{}, 特殊符号:{}", s, sym);
|
|
|
+ flag = true;
|
|
|
+ // 取出现特殊符号前的值查询
|
|
|
+ s = StrUtil.subBefore(s, sym, true);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ builder.append(s);
|
|
|
+ if (flag){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ i ++;
|
|
|
+
|
|
|
+ }
|
|
|
+ String out = builder.toString();
|
|
|
+ log.info("out:{}", out);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String str = "我·是 中—国(人), 你-在{干嘛}--哈—哈。 ddd.jpg";
|
|
|
+ System.out.println(specificSymbol(str));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test(){
|
|
|
+// String regEx = "12,15,+ delete";
|
|
|
+// regSql(regEx);
|
|
|
+ System.out.println("'".contains("Monk's"));
|
|
|
+ System.out.println("Monk's".contains("'"));
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|