1. 程式人生 > 程式設計 >簡單瞭解Mybatis如何實現SQL防注入

簡單瞭解Mybatis如何實現SQL防注入

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

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

什麼是SQL注入

在討論怎麼實現之前,首先了解一下什麼是SQL注入,我們有一個簡單的查詢操作:根據id查詢一個使用者資訊。它的sql語句應該是這樣:select * from user where id =。我們根據傳入條件填入id進行查詢。

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

但是如果傳入的引數變成'' or 1=1,這時這條語句變成select * from user where id = '' or 1=1。讓我們想一下這條語句的執行結果會是怎麼?它會將我們使用者表中所有的資料查詢出來,顯然這是一個大的錯誤。這就是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>

當我們在外部對這兩個方法繼續呼叫時,發現如果傳入安全的引數時,兩者結果並無不同,如果傳入不安全的引數時,第一種使用#的方法查詢不到結果(select * from user where id = '' or 1=1),但這個引數在第二種也就是$下會得到全部的結果。

並且如果我們將sql進行列印,會發現新增#時,向資料庫執行的sql為:select * from user where id = ' \'\' or 1=1 ',它會在我們的引數外再加一層引號,在使用$時,它的執行sql是select * from user where id = '' or 1=1。

棄用$可以嗎

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

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

如何實現SQL注入的,不用Mybatis怎麼實現

其實Mybatis也是通過jdbc來進行資料庫連線的,如果我們看一下jdbc的使用,就可以得到這個原因。

#使用了PreparedStatement來進行預處理,然後通過set的方式對佔位符進行設定,而$則是通過Statement直接進行查詢,當有引數時直接拼接進行查詢。

所以說我們可以使用jdbc來實現SQL注入。

看一下這兩個的程式碼:

public static void statement(Connection connection) {
 System.out.println("statement-----");
 String selectSql = "select * from user";
 // 相當於mybatis中使用$,拿到引數後直接拼接
 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中的#,先對要執行的sql進行預處理,設定佔位符,然後設定引數
 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));
 }
}

總結

  • Mybatis中使用#可以防止SQL注入,$並不能防止SQL注入
  • Mybatis實現SQL注入的原理是呼叫了jdbc中的PreparedStatement來進行預處理。

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