欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java項(xiàng)目中防止SQL注入的四種方法推薦

 更新時(shí)間:2025年03月06日 10:04:13   作者:Javaの甘乃迪  
sql注入是web開(kāi)發(fā)中最常見(jiàn)的一種安全漏洞,這篇文章為大家整理了四種Java項(xiàng)目中防止SQL注入的方法,有需要的小伙伴可以參考一下

一、什么是SQL注入

SQL注入即是指web應(yīng)用程序?qū)τ脩?hù)輸入數(shù)據(jù)的合法性沒(méi)有判斷或過(guò)濾不嚴(yán),攻擊者可以在web應(yīng)用程序中事先定義好的查詢(xún)語(yǔ)句的結(jié)尾上添加額外的SQL語(yǔ)句,在管理員不知情的情況下實(shí)現(xiàn)非法操作,以此來(lái)實(shí)現(xiàn)欺騙數(shù)據(jù)庫(kù)服務(wù)器執(zhí)行非授權(quán)的任意查詢(xún),從而進(jìn)一步得到相應(yīng)的數(shù)據(jù)信息。

SQL案列:

String sql = "delete from table1 where id = " + "id";

這個(gè)id從請(qǐng)求參數(shù)中獲取,若參數(shù)被拼接為:

"1001 or  1 = 1"

最執(zhí)行語(yǔ)句變?yōu)椋?/p>

String sql = "delete from table1 where id = 1001 or 1 = 1";

此時(shí),數(shù)據(jù)庫(kù)的數(shù)據(jù)都會(huì)被清空掉,后果非常嚴(yán)重

二、Java項(xiàng)目防止SQL注入方式

這里總結(jié)4種:

  • PreparedStatement防止SQL注入
  • mybatis中#{}防止SQL注入
  • 對(duì)請(qǐng)求參數(shù)的敏感詞匯進(jìn)行過(guò)濾
  • nginx反向代理防止SQL注入

1、PreparedStatement防止SQL注入

PreparedStatement具有預(yù)編譯功能,以上述SQL為例

使用PreparedStatement預(yù)編譯后的SQL為:

delete from table1 where id= ?

此時(shí)SQL語(yǔ)句結(jié)構(gòu)已固定,無(wú)論"?"被替換為任何參數(shù),SQL語(yǔ)句只認(rèn)為where后面只有一個(gè)條件,當(dāng)再傳入 1001 or  1 = 1時(shí),語(yǔ)句會(huì)報(bào)錯(cuò),從而達(dá)到防止SQL注入效果。

2、mybatis中#{}防止SQL注入

mybatis中#{}表達(dá)式防止SQL注入與PreparedStatement類(lèi)似,都是對(duì)SQL語(yǔ)句進(jìn)行預(yù)編譯處理

PS 注意:

#{} :參數(shù)占位符

${} :拼接替換符,不能防止SQL注入,一般用于:

  • 傳入數(shù)據(jù)庫(kù)對(duì)象(如:數(shù)據(jù)庫(kù)名稱(chēng)、表名)
  • order by  后的條件

3、對(duì)請(qǐng)求參數(shù)的敏感詞匯進(jìn)行過(guò)濾

這里是springboot的寫(xiě)法,如下:

import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.Enumeration;
 
/**
 * @Auther: admin
 * @Description: sql防注入過(guò)濾器
 */
@WebFilter(urlPatterns = "/*",filterName = "sqlFilter")
@Configuration
public class SqlFilter implements Filter {
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}
 
    /**
     * @description sql注入過(guò)濾
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest request = servletRequest;
        ServletResponse response = servletResponse;
        // 獲得所有請(qǐng)求參數(shù)名
        Enumeration<String> names = request.getParameterNames();
        String sql = "";
        while (names.hasMoreElements()){
            // 得到參數(shù)名
            String name = names.nextElement().toString();
            // 得到參數(shù)對(duì)應(yīng)值
            String[] values = request.getParameterValues(name);
            for (int i = 0; i < values.length; i++) {
                sql += values[i];
            }
        }
        if (sqlValidate(sql)) {
            //TODO 這里直接拋異常處理,前后端交互項(xiàng)目中,請(qǐng)把錯(cuò)誤信息按前后端"數(shù)據(jù)返回的VO"對(duì)象進(jìn)行封裝
            throw new IOException("您發(fā)送請(qǐng)求中的參數(shù)中含有非法字符");
        } else {
            filterChain.doFilter(request,response);
        }
    }
 
    /**
     * @description 匹配效驗(yàn)
     */
    protected static boolean sqlValidate(String str){
        // 統(tǒng)一轉(zhuǎn)為小寫(xiě)
        String s = str.toLowerCase();
        // 過(guò)濾掉的sql關(guān)鍵字,特殊字符前面需要加\\進(jìn)行轉(zhuǎn)義
        String badStr =
                "select|update|and|or|delete|insert|truncate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute|table|"+
                        "char|declare|sitename|xp_cmdshell|like|from|grant|use|group_concat|column_name|" +
                        "information_schema.columns|table_schema|union|where|order|by|" +
                        "'\\*|\\;|\\-|\\--|\\+|\\,|\\//|\\/|\\%|\\#";
        //使用正則表達(dá)式進(jìn)行匹配
        boolean matches = s.matches(badStr);
        return matches;
    }
 
    @Override
    public void destroy() {}
}

4、nginx反向代理防止SQL注入 

越來(lái)越多網(wǎng)站使用nginx進(jìn)行反向代理,該層我們也可以進(jìn)行防止SQL注入配置。

將下面的Nginx配置文件代碼放入到server塊中,然后重啟Nginx即可。

 if ($request_method !~* GET|POST) { return 444; }
 #使用444錯(cuò)誤代碼可以更加減輕服務(wù)器負(fù)載壓力。
 #防止SQL注入
 if ($query_string ~* (\$|'|--|[+|(%20)]union[+|(%20)]|[+|(%20)]insert[+|(%20)]|[+|(%20)]drop[+|(%20)]|[+|(%20)]truncate[+|(%20)]|[+|(%20)]update[+|(%20)]|[+|(%20)]from[+|(%20)]|[+|(%20)]grant[+|(%20)]|[+|(%20)]exec[+|(%20)]|[+|(%20)]where[+|(%20)]|[+|(%20)]select[+|(%20)]|[+|(%20)]and[+|(%20)]|[+|(%20)]or[+|(%20)]|[+|(%20)]count[+|(%20)]|[+|(%20)]exec[+|(%20)]|[+|(%20)]chr[+|(%20)]|[+|(%20)]mid[+|(%20)]|[+|(%20)]like[+|(%20)]|[+|(%20)]iframe[+|(%20)]|[\<|%3c]script[\>|%3e]|javascript|alert|webscan|dbappsecurity|style|confirm\(|innerhtml|innertext)(.*)$) { return 555; }
 if ($uri ~* (/~).*) { return 501; }
 if ($uri ~* (\\x.)) { return 501; }
 #防止SQL注入 
 if ($query_string ~* "[;'<>].*") { return 509; }
 if ($request_uri ~ " ") { return 509; }
 if ($request_uri ~ (\/\.+)) { return 509; }
 if ($request_uri ~ (\.+\/)) { return 509; }
 #if ($uri ~* (insert|select|delete|update|count|master|truncate|declare|exec|\*|\')(.*)$ ) { return 503; }
 #防止SQL注入
 if ($request_uri ~* "(cost\()|(concat\()") { return 504; }
 if ($request_uri ~* "[+|(%20)]union[+|(%20)]") { return 504; }
 if ($request_uri ~* "[+|(%20)]and[+|(%20)]") { return 504; }
 if ($request_uri ~* "[+|(%20)]select[+|(%20)]") { return 504; }
 if ($request_uri ~* "[+|(%20)]or[+|(%20)]") { return 504; }
 if ($request_uri ~* "[+|(%20)]delete[+|(%20)]") { return 504; }
 if ($request_uri ~* "[+|(%20)]update[+|(%20)]") { return 504; }
 if ($request_uri ~* "[+|(%20)]insert[+|(%20)]") { return 504; }
 if ($query_string ~ "(<|%3C).*script.*(>|%3E)") { return 505; }
 if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") { return 505; }
 if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") { return 505; }
 if ($query_string ~ "proc/self/environ") { return 505; }
 if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") { return 505; }
 if ($query_string ~ "base64_(en|de)code\(.*\)") { return 505; }
 if ($query_string ~ "[a-zA-Z0-9_]=http://") { return 506; }
 if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") { return 506; }
 if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") { return 506; }
 if ($query_string ~ "b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)b") { return 507; }
 if ($query_string ~ "b(erections|hoodia|huronriveracres|impotence|levitra|libido)b") {return 507; }
 if ($query_string ~ "b(ambien|bluespill|cialis|cocaine|ejaculation|erectile)b") { return 507; }
 if ($query_string ~ "b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)b") { return 507; }
 #這里大家根據(jù)自己情況添加刪減上述判斷參數(shù),cURL、wget這類(lèi)的屏蔽有點(diǎn)兒極端了,但要“寧可錯(cuò)殺一千,不可放過(guò)一個(gè)”。
 if ($http_user_agent ~* YisouSpider|ApacheBench|WebBench|Jmeter|JoeDog|Havij|GetRight|TurnitinBot|GrabNet|masscan|mail2000|github|wget|curl|Java|python) { return 508; }
 #同上,大家根據(jù)自己站點(diǎn)實(shí)際情況來(lái)添加刪減下面的屏蔽攔截參數(shù)。
 if ($http_user_agent ~* "Go-Ahead-Got-It") { return 508; }
 if ($http_user_agent ~* "GetWeb!") { return 508; }
 if ($http_user_agent ~* "Go!Zilla") { return 508; }
 if ($http_user_agent ~* "Download Demon") { return 508; }
 if ($http_user_agent ~* "Indy Library") { return 508; }
 if ($http_user_agent ~* "libwww-perl") { return 508; }
 if ($http_user_agent ~* "Nmap Scripting Engine") { return 508; }
 if ($http_user_agent ~* "~17ce.com") { return 508; }
 if ($http_user_agent ~* "WebBench*") { return 508; }
 if ($http_user_agent ~* "spider") { return 508; } #這個(gè)會(huì)影響國(guó)內(nèi)某些搜索引擎爬蟲(chóng),比如:搜狗
 #攔截各惡意請(qǐng)求的UA,可以通過(guò)分析站點(diǎn)日志文件或者waf日志作為參考配置。
 if ($http_referer ~* 17ce.com) { return 509; }
 #攔截17ce.com站點(diǎn)測(cè)速節(jié)點(diǎn)的請(qǐng)求,所以明月一直都說(shuō)這些測(cè)速網(wǎng)站的數(shù)據(jù)僅供參考不能當(dāng)真的。
 if ($http_referer ~* WebBench*") { return 509; }
 #攔截WebBench或者類(lèi)似壓力測(cè)試工具,其他工具只需要更換名稱(chēng)即可。

到此這篇關(guān)于Java項(xiàng)目中防止SQL注入的四種方法推薦的文章就介紹到這了,更多相關(guān)Java防止SQL注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring boot validation校驗(yàn)方法實(shí)例

    Spring boot validation校驗(yàn)方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Spring boot validation校驗(yàn)方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Spring加載properties文件的方法

    Spring加載properties文件的方法

    這篇文章主要為大家詳細(xì)介紹了Spring加載properties文件的兩種方法,一是通過(guò)xml方式,另一種方式是通過(guò)注解方式,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Java入門(mén)交換數(shù)組中兩個(gè)元素的位置

    Java入門(mén)交換數(shù)組中兩個(gè)元素的位置

    在Java中,交換數(shù)組中的兩個(gè)元素是基本的數(shù)組操作,下面我們將詳細(xì)介紹如何實(shí)現(xiàn)這一操作,以及在實(shí)際應(yīng)用中這種技術(shù)的重要性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 詳解java設(shè)計(jì)模式中的門(mén)面模式

    詳解java設(shè)計(jì)模式中的門(mén)面模式

    門(mén)面模式又叫外觀模式(Facade?Pattern),主要用于隱藏系統(tǒng)的復(fù)雜性,并向客戶(hù)端提供了一個(gè)客戶(hù)端可以訪(fǎng)問(wèn)系統(tǒng)的接口,本文通過(guò)實(shí)例代碼給大家介紹下java門(mén)面模式的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2022-02-02
  • java的依賴(lài)倒置原則你了解多少

    java的依賴(lài)倒置原則你了解多少

    這篇文章主要為大家詳細(xì)介紹了java的依賴(lài)倒置原則,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • Java通過(guò)SMS短信平臺(tái)實(shí)現(xiàn)發(fā)短信功能 含多語(yǔ)言

    Java通過(guò)SMS短信平臺(tái)實(shí)現(xiàn)發(fā)短信功能 含多語(yǔ)言

    這篇文章主要為大家詳細(xì)介紹了Java通過(guò)SMS短信平臺(tái)實(shí)現(xiàn)發(fā)短信功能的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Spring框架學(xué)習(xí)常用注解匯總

    Spring框架學(xué)習(xí)常用注解匯總

    這篇文章主要為大家介紹了Spring框架學(xué)習(xí)中一些經(jīng)常用的注解匯總及示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • 解決mybatis resultMap根據(jù)type找不到對(duì)應(yīng)的包問(wèn)題

    解決mybatis resultMap根據(jù)type找不到對(duì)應(yīng)的包問(wèn)題

    這篇文章主要介紹了解決mybatis resultMap根據(jù)type找不到對(duì)應(yīng)的包問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 使用java + OpenCV破解頂象面積驗(yàn)證碼的示例

    使用java + OpenCV破解頂象面積驗(yàn)證碼的示例

    這篇文章主要介紹了使用java + OpenCV破解頂象面積驗(yàn)證碼的示例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • SpringBoot動(dòng)態(tài)修改日志級(jí)別的操作

    SpringBoot動(dòng)態(tài)修改日志級(jí)別的操作

    這篇文章主要介紹了SpringBoot動(dòng)態(tài)修改日志級(jí)別的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評(píng)論