Struts2学习4——防sql注入过滤器

Posted 编程圈子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2学习4——防sql注入过滤器相关的知识,希望对你有一定的参考价值。

web.xml

    <!-- sql Filter -->
     <filter>
        <filter-name>SqlFilter</filter-name>
        <filter-class>Utils.SqlFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SqlFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Utils.SqlFilter

package Utils;

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;

//过滤sql关键字的Filter
public class SqlFilter implements Filter 

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException 

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        // 获得所有请求参数名
        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);
        if (sqlValidate(sql)) 
            res.sendRedirect("/error.jsp");
         else 
            chain.doFilter(req, res);
        
    

    // 校验
    protected static boolean sqlValidate(String str) 
        str = str.toLowerCase();// 统一转为小写
        // String badStr = "and|exec";
        String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|chr|mid|master|truncate|char|declare|sitename|net user|xp_cmdshell|or|like";
        /*
         * String badStr =
         * "'|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]) != -1) 
                System.out.println("匹配到:" + badStrs[i]);
                return true;
            
        
        return false;
    

    public void init(FilterConfig filterConfig) throws ServletException 
        // throw new UnsupportedOperationException("Not supported yet.");
    

    public void destroy() 
        // throw new UnsupportedOperationException("Not supported yet.");
    

另外程序代码中避免字符串拼接,使用PreparedStatement。

原文参考:
http://blog.csdn.net/u012377333/article/details/51535483

以上是关于Struts2学习4——防sql注入过滤器的主要内容,如果未能解决你的问题,请参考以下文章

防sql注入过滤器

Java 防SQL注入过滤器(拦截器)代码

TP5框架 《防sql注入、防xss攻击》

php防sql注入

PHP防XSS 防SQL注入的代码

springboot安全组件总结