Java防止SQL注入2(通过filter过滤器功能进行拦截)

首先说明一点,这个过滤器拦截其实是不靠谱的,比如说我的一篇文章是介绍sql注入的,或者评论的内容是有关sql的,那会过滤掉;且如果每个页面都经过这个过滤器,那么效率也是非常低的。

如果是要SQL注入拦截,可以在数据访问上层的业务层用方法的形式进行手动过滤还靠谱些。

或者使用SQL的参数形式进行,这个绝对是百分百搞得定。

关于SQL注入解释,参考:http://www.cnblogs.com/EasonJim/p/6223216.html

关于filter过滤器使用教程,参考:http://www.runoob.com/servlet/servlet-writing-filters.html

使用filter过滤器实现的代码如下:

web.xml:

<!--在web.xml文件中的配置-->
<!-- 防止SQL注入的过滤器 -->
<filter>
    <filter-name>antiSqlInjection</filter-name>
    <filter-class>com.tarena.dingdang.filter.AntiSqlInjectionfilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>antiSqlInjection</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

filter:

package com.jsoft.jblog.filter;
 
import java.io.IOException;
import java.util.Enumeration;
 

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class AntiSqlInjectionfilter implements Filter {
 
    public void destroy() {
        // TODO Auto-generated method stub
    }
     
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
    }
     
    public void doFilter(ServletRequest args0, ServletResponse args1,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest)args0;
        HttpServletResponse res=(HttpServletResponse)args1;
         //获得所有请求参数名
        Enumeration params = req.getParameterNames();
        String sql = "";
        while (params.hasMoreElements()) {
            //得到参数名
            String name = params.nextElement().toString();
            //System.out.println("name===========================" + name + "--");
            //得到参数对应值
            String[] value = req.getParameterValues(name);
            for (int i = 0; i < value.length; i++) {
                sql = sql + value[i];
            }
        }
        //System.out.println("============================SQL"+sql);
        //有sql关键字,跳转到error.html
        if (sqlValidate(sql)) {
            throw new IOException("您发送请求中的参数中含有非法字符");
            //String ip = req.getRemoteAddr();
        } else {
            chain.doFilter(args0,args1);
        }
    }
     
    //效验
    protected static boolean sqlValidate(String str) {
        str = str.toLowerCase();//统一转为小写
        String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|*|%|chr|mid|master|truncate|" +
                "char|declare|sitename|net user|xp_cmdshell|;|or|-|+|,|like'|and|exec|execute|insert|create|drop|" +
                "table|from|grant|use|group_concat|column_name|" +
                "information_schema.columns|table_schema|union|where|select|delete|update|order|by|count|*|" +
                "chr|mid|master|truncate|char|declare|or|;|-|--|+|,|like|//|/|%|#";//过滤掉的sql关键字,可以手动添加
        String[] badStrs = badStr.split("\\|");
        for (int i = 0; i < badStrs.length; i++) {
            if (str.indexOf(badStrs[i]) >= 0) {
                return true;
            }
        }
        return false;
    }
}

参考:http://www.oschina.net/code/snippet_811941_14131

posted @ 2016-12-26 22:38  EasonJim  阅读(14586)  评论(0编辑  收藏  举报