提交 | 用户 | 时间
|
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注入过滤 |
|
15 |
* @param str 待验证的字符串 |
|
16 |
*/ |
|
17 |
public static String sqlInject(String str){ |
|
18 |
if(StringUtils.isBlank(str)){ |
|
19 |
return null; |
|
20 |
} |
|
21 |
//去掉'|"|;|\字符 |
|
22 |
str = StringUtils.replace(str, "'", ""); |
|
23 |
str = StringUtils.replace(str, "\"", ""); |
|
24 |
str = StringUtils.replace(str, ";", ""); |
|
25 |
str = StringUtils.replace(str, "\\", ""); |
|
26 |
|
|
27 |
//转换成小写 |
|
28 |
str = str.toLowerCase(); |
|
29 |
|
|
30 |
//非法字符 |
|
31 |
String[] keywords = {"master", "truncate", "insert", "select", "delete", "update", "declare", "alter", "drop"}; |
|
32 |
|
|
33 |
//判断是否包含非法字符 |
|
34 |
for(String keyword : keywords){ |
|
35 |
if(str.indexOf(keyword) != -1){ |
|
36 |
throw new RRException("包含非法字符"); |
|
37 |
} |
|
38 |
} |
|
39 |
|
|
40 |
return str; |
|
41 |
} |
|
42 |
} |