preparedstatement打印sql语句_Mybatis是这样防止sql注入的
鏈接:https://juejin.im/post/5e131203e51d4541082c7db3
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來進行預處理。
確認過眼神
總結
以上是生活随笔為你收集整理的preparedstatement打印sql语句_Mybatis是这样防止sql注入的的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python定义字符串数组_python
- 下一篇: bugku 杂项 就五层你能解开吗_9.