雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

Java

當前位置:主頁 > 軟件編程 > Java >

簡單了解Mybatis如何實現(xiàn)SQL防注入

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Java|點擊:

這篇文章主要介紹了簡單了解Mybatis如何實現(xiàn)SQL防注入,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Mybatis這個框架在日常開發(fā)中用的很多,比如面試中經(jīng)常有一個問題:$和#的區(qū)別,它們的區(qū)別是使用#可以防止SQL注入,今天就來看一下它是如何實現(xiàn)SQL注入的。

什么是SQL注入

在討論怎么實現(xiàn)之前,首先了解一下什么是SQL注入,我們有一個簡單的查詢操作:根據(jù)id查詢一個用戶信息。它的sql語句應(yīng)該是這樣:select * from user where id =。我們根據(jù)傳入條件填入id進行查詢。

如果正常操作,傳入一個正常的id,比如說2,那么這條語句變成select * from user where id =2。這條語句是可以正常運行并且符合我們預(yù)期的。

但是如果傳入的參數(shù)變成'' or 1=1,這時這條語句變成select * from user where id = '' or 1=1。讓我們想一下這條語句的執(zhí)行結(jié)果會是怎么?它會將我們用戶表中所有的數(shù)據(jù)查詢出來,顯然這是一個大的錯誤。這就是SQL注入。

Mybatis如何防止SQL注入

在開頭講過,可以使用#來防止SQL注入,它的寫法如下:

<select id="safeSelect" resultMap="testUser">
  SELECT * FROM user where id = #{id}
</select>

在mybatis中查詢還有一個寫法是使用$,它的寫法如下:

<select id="unsafeSelect" resultMap="testUser">
  select * from user where id = ${id}
</select>

當我們在外部對這兩個方法繼續(xù)調(diào)用時,發(fā)現(xiàn)如果傳入安全的參數(shù)時,兩者結(jié)果并無不同,如果傳入不安全的參數(shù)時,第一種使用#的方法查詢不到結(jié)果(select * from user where id = '' or 1=1),但這個參數(shù)在第二種也就是$下會得到全部的結(jié)果。

并且如果我們將sql進行打印,會發(fā)現(xiàn)添加#時,向數(shù)據(jù)庫執(zhí)行的sql為:select * from user where id = ' \'\' or 1=1 ',它會在我們的參數(shù)外再加一層引號,在使用$時,它的執(zhí)行sql是select * from user where id = '' or 1=1。

棄用$可以嗎

我們使用#也能完成$的作用,并且使用$還有危險,那么我們以后不使用$不就行了嗎。

并不是,它只是在我們這種場景下會有問題,但是在有一些動態(tài)查詢的場景中還是有不可代替的作用的,比如,動態(tài)修改表名select * from ${table} where id = #{id}。我們就可以在返回信息一致的情況下進行動態(tài)的更改查詢的表,這也是mybatis動態(tài)強大的地方。

如何實現(xiàn)SQL注入的,不用Mybatis怎么實現(xiàn)

其實Mybatis也是通過jdbc來進行數(shù)據(jù)庫連接的,如果我們看一下jdbc的使用,就可以得到這個原因。

#使用了PreparedStatement來進行預(yù)處理,然后通過set的方式對占位符進行設(shè)置,而$則是通過Statement直接進行查詢,當有參數(shù)時直接拼接進行查詢。

所以說我們可以使用jdbc來實現(xiàn)SQL注入。

看一下這兩個的代碼:

public static void statement(Connection connection) {
 System.out.println("statement-----");
 String selectSql = "select * from user";
 // 相當于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;";
 //相當于mybatis中的#,先對要執(zhí)行的sql進行預(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實現(xiàn)SQL注入的原理是調(diào)用了jdbc中的PreparedStatement來進行預(yù)處理。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:Java服務(wù)器端跨域問題解決方案

欄    目:Java

下一篇:Java web攔截器inteceptor原理及應(yīng)用詳解

本文標題:簡單了解Mybatis如何實現(xiàn)SQL防注入

本文地址:http://www.jygsgssxh.com/a1/Java/8733.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有