给已经上线的项目写了一个防sql注入的过滤器,并打成jar包放在项目的lib目录下。
package com.security.web.security.filters;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
public class MyFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest reqs, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
System.out.println("=======================过滤器接收请求");
System.out.println("=======================接收请求");
System.out.println("=======================接收请求");
HttpServletRequest request = (HttpServletRequest) reqs;
HttpServletResponse response = (HttpServletResponse) resp;
// 获得所有请求参数名
Enumeration params = request.getParameterNames();
String sql = "";
while (params.hasMoreElements()) {
// 得到参数名
String name = params.nextElement().toString();
System.out.println("=======================参数name:"+name);
// 得到参数对应值
String[] value = request.getParameterValues(name);
if (null != value){
for (int i = 0; i < value.length; i++) {
System.out.println("=======================name对应的value:"+value[i]);
// sql = sql + value[i];
//判断每一个string
if (sqlValidate2(value[i])){
System.out.println("=======================在for循环里面拦截非法字符!");
System.out.println("=======================在for循环里面拦截非法字符!");
System.out.println("=======================在for循环里面拦截非法字符!");
throw new IOException("您发送请求中的参数中含有非法字符");
}else {
sql = sql + value[i];
}
}
}
}
if (sqlValidate2(sql)) {
System.out.println("=======================在外面拦截非法字符!");
System.out.println("=======================在外面拦截非法字符!");
System.out.println("=======================在外面拦截非法字符!");
throw new IOException("您发送请求中的参数中含有非法字符");
} else {
chain.doFilter(request, response);
}
}
public void destroy() {
}
/**
* 参数校验
* @param str
*/
public static boolean sqlValidate1(String str) {
str = str.toLowerCase();//统一转为小写
String badStr = "select|update|delete|insert|truncate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute|table";
String[] badStrs = badStr.split("|");
for (int i = 0; i < badStrs.length; i++) {
//循环检测,判断在请求参数当中是否包含SQL关键字
if (str.indexOf(badStrs[i]) >= 0) {
return true;
}
}
return false;
}
//效验
protected static boolean sqlValidate2(String str) {
str = str.toLowerCase();//统一转为小写,去空格
String badStr = "exec|execute|insert|select|delete|update|drop|*|chr|mid|master|truncate|" +
"char|declare|sitename|net user|xp_cmdshell|;|-|+|create|" +
"table|from|grant|use|group_concat|column_name|" +
"information_schema.columns|table_schema|union|where|order|by|count|" +
"--|//|/|#|or 1 = 1|or '|' or|or'|'or|)or|) or|or(|or (| or|or | and|and |)and|and(|,and|and'";//过滤掉的sql关键字,可以手动添加
String[] badStrs = badStr.split("\\|");
for (int i = 0; i < badStrs.length; i++) {
if (str.indexOf(badStrs[i]) >= 0) {
return true;
}
}
return false;
}
}
在web.xml中配置过滤器:
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.security.web.security.filters.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>