JSP使用過濾器防止SQL注入

來源:互聯網
上載者:User

標籤:

package com;
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.");  
    }  
}

JSP使用過濾器防止SQL注入

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.