dongyukun
2025-01-24 5c2fc8b317e17d0b194f27937bd9d2af8961502f
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.common.xss;
H 2
3 import com.iailab.module.data.common.exception.RRException;
4 import org.apache.commons.lang3.StringUtils;
5
6 /**
7  * SQL过滤
8  *
9  * @author Mark sunlightcs@gmail.com
10  */
11 public class SQLFilter {
12
13     /**
14      * SQL注入过滤
619626 15      *
16      * @param str 待验证的字符串
a6de49 17      */
619626 18     public static String sqlInject(String str) {
19         if (StringUtils.isBlank(str)) {
a6de49 20             return null;
H 21         }
22         //去掉'|"|;|\字符
23         str = StringUtils.replace(str, "'", "");
24         str = StringUtils.replace(str, "\"", "");
25         str = StringUtils.replace(str, ";", "");
26         str = StringUtils.replace(str, "\\", "");
27
28         //转换成小写
29         str = str.toLowerCase();
30
31         //非法字符
32         String[] keywords = {"master", "truncate", "insert", "select", "delete", "update", "declare", "alter", "drop"};
33
34         //判断是否包含非法字符
619626 35         for (String keyword : keywords) {
36             if (str.indexOf(keyword) != -1) {
37                 throw new RRException("包含非法字符");
38             }
39         }
40
41         return str;
42     }
43
44     /**
45      * SQL注入过滤
46      *
47      * @param orgStr 待验证的字符串
48      */
49     public static String sqlInject2(String orgStr) {
50         if (StringUtils.isBlank(orgStr)) {
51             return null;
52         }
53         //转换成小写
54         String str = new String(orgStr.toLowerCase());
55
56         //非法字符
55beca 57         String[] keywords = {";", "master", "truncate", "insert", "delete", "update", "declare", "alter", "drop"};
619626 58
59         //判断是否包含非法字符
60         for (String keyword : keywords) {
61             if (str.indexOf(keyword) != -1) {
a6de49 62                 throw new RRException("包含非法字符");
H 63             }
64         }
65
66         return str;
67     }
68 }