簡(jiǎn)單了解Mybatis如何實(shí)現(xiàn)SQL防注入
這篇文章主要介紹了簡(jiǎn)單了解Mybatis如何實(shí)現(xiàn)SQL防注入,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
Mybatis這個(gè)框架在日常開(kāi)發(fā)中用的很多,比如面試中經(jīng)常有一個(gè)問(wèn)題:$和#的區(qū)別,它們的區(qū)別是使用#可以防止SQL注入,今天就來(lái)看一下它是如何實(shí)現(xiàn)SQL注入的。
什么是SQL注入
在討論怎么實(shí)現(xiàn)之前,首先了解一下什么是SQL注入,我們有一個(gè)簡(jiǎn)單的查詢操作:根據(jù)id查詢一個(gè)用戶信息。它的sql語(yǔ)句應(yīng)該是這樣:select * from user where id =。我們根據(jù)傳入條件填入id進(jìn)行查詢。
如果正常操作,傳入一個(gè)正常的id,比如說(shuō)2,那么這條語(yǔ)句變成select * from user where id =2。這條語(yǔ)句是可以正常運(yùn)行并且符合我們預(yù)期的。
但是如果傳入的參數(shù)變成'' or 1=1,這時(shí)這條語(yǔ)句變成select * from user where id = '' or 1=1。讓我們想一下這條語(yǔ)句的執(zhí)行結(jié)果會(huì)是怎么?它會(huì)將我們用戶表中所有的數(shù)據(jù)查詢出來(lái),顯然這是一個(gè)大的錯(cuò)誤。這就是SQL注入。
Mybatis如何防止SQL注入
在開(kāi)頭講過(guò),可以使用#來(lái)防止SQL注入,它的寫法如下:
<select id="safeSelect" resultMap="testUser"> SELECT * FROM user where id = #{id} </select>
在mybatis中查詢還有一個(gè)寫法是使用$,它的寫法如下:
<select id="unsafeSelect" resultMap="testUser"> select * from user where id = ${id} </select>
當(dāng)我們?cè)谕獠繉?duì)這兩個(gè)方法繼續(xù)調(diào)用時(shí),發(fā)現(xiàn)如果傳入安全的參數(shù)時(shí),兩者結(jié)果并無(wú)不同,如果傳入不安全的參數(shù)時(shí),第一種使用#的方法查詢不到結(jié)果(select * from user where id = '' or 1=1),但這個(gè)參數(shù)在第二種也就是$下會(huì)得到全部的結(jié)果。
并且如果我們將sql進(jìn)行打印,會(huì)發(fā)現(xiàn)添加#時(shí),向數(shù)據(jù)庫(kù)執(zhí)行的sql為:select * from user where id = ' \'\' or 1=1 ',它會(huì)在我們的參數(shù)外再加一層引號(hào),在使用$時(shí),它的執(zhí)行sql是select * from user where id = '' or 1=1。
棄用$可以嗎
我們使用#也能完成$的作用,并且使用$還有危險(xiǎn),那么我們以后不使用$不就行了嗎。
并不是,它只是在我們這種場(chǎng)景下會(huì)有問(wèn)題,但是在有一些動(dòng)態(tài)查詢的場(chǎng)景中還是有不可代替的作用的,比如,動(dòng)態(tài)修改表名select * from ${table} where id = #{id}。我們就可以在返回信息一致的情況下進(jìn)行動(dòng)態(tài)的更改查詢的表,這也是mybatis動(dòng)態(tài)強(qiáng)大的地方。
如何實(shí)現(xiàn)SQL注入的,不用Mybatis怎么實(shí)現(xiàn)
其實(shí)Mybatis也是通過(guò)jdbc來(lái)進(jìn)行數(shù)據(jù)庫(kù)連接的,如果我們看一下jdbc的使用,就可以得到這個(gè)原因。
#使用了PreparedStatement來(lái)進(jìn)行預(yù)處理,然后通過(guò)set的方式對(duì)占位符進(jìn)行設(shè)置,而$則是通過(guò)Statement直接進(jìn)行查詢,當(dāng)有參數(shù)時(shí)直接拼接進(jìn)行查詢。
所以說(shuō)我們可以使用jdbc來(lái)實(shí)現(xiàn)SQL注入。
看一下這兩個(gè)的代碼:
public static void statement(Connection connection) { System.out.println("statement-----"); String selectSql = "select * from user"; // 相當(dāng)于mybatis中使用$,拿到參數(shù)后直接拼接 String unsafeSql = "select * from user where id = '' or 1=1;"; Statement statement = null; try { statement = connection.createStatement(); } catch (SQLException e) { e.printStackTrace(); } try { ResultSet resultSet = statement.executeQuery(selectSql); print(resultSet); } catch (SQLException e) { e.printStackTrace(); } System.out.println("---****---"); try { ResultSet resultSet = statement.executeQuery(unsafeSql); print(resultSet); } catch (SQLException e) { e.printStackTrace(); } } public static void preparedStatement(Connection connection) { System.out.println("preparedStatement-----"); String selectSql = "select * from user;"; //相當(dāng)于mybatis中的#,先對(duì)要執(zhí)行的sql進(jìn)行預(yù)處理,設(shè)置占位符,然后設(shè)置參數(shù) String safeSql = "select * from user where id =?;"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(selectSql); ResultSet resultSet = preparedStatement.executeQuery(); print(resultSet); } catch (SQLException e) { e.printStackTrace(); } System.out.println("---****---"); try { preparedStatement = connection.prepareStatement(safeSql); preparedStatement.setString(1," '' or 1 = 1 "); ResultSet resultSet = preparedStatement.executeQuery(); print(resultSet); } catch (SQLException e) { e.printStackTrace(); } } public static void print(ResultSet resultSet) throws SQLException { while (resultSet.next()) { System.out.print(resultSet.getString(1) + ", "); System.out.print(resultSet.getString("name") + ", "); System.out.println(resultSet.getString(3)); } }
總結(jié)
- Mybatis中使用#可以防止SQL注入,$并不能防止SQL注入
- Mybatis實(shí)現(xiàn)SQL注入的原理是調(diào)用了jdbc中的PreparedStatement來(lái)進(jìn)行預(yù)處理。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項(xiàng)目部署到服務(wù)器上的方法(Jar包)
這篇文章主要介紹了SpringBoot項(xiàng)目部署到服務(wù)器上的方法(Jar包),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Java計(jì)時(shí)新姿勢(shì)StopWatch的使用方法詳解
這篇文章主要給大家介紹了關(guān)于Java計(jì)時(shí)新姿勢(shì)StopWatch的相關(guān)資料,以及java 中使用StopWatch來(lái)計(jì)算時(shí)間差的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01springboot+nginx+https+linux實(shí)現(xiàn)負(fù)載均衡加域名訪問(wèn)簡(jiǎn)單測(cè)試
這篇文章主要介紹了springboot+nginx+https+linux實(shí)現(xiàn)負(fù)載均衡加域名訪問(wèn)簡(jiǎn)單測(cè)試,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05

JAVA Iterator 轉(zhuǎn)成 List 的操作