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