【转】从源码分析PreparedStatement是如何防止SQL注入的?
為什么在Java中PreparedStatement能夠有效防止SQL注入?這可能是每個Java程序員思考過的問題。
?
首先我們來看下直觀的現象(注:需要提前打開mysql的SQL文日志)
1. 不使用PreparedStatement的set方法設置參數(效果跟Statement相似,相當于執行靜態SQL)
String param = "'test' or 1=1"; String sql = "select file from file where name = " + param; // 拼接SQL參數 PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); System.out.println(resultSet.next());輸出結果為true,DB中執行的SQL為
-- 永真條件1=1成為了查詢條件的一部分,可以返回所有數據,造成了SQL注入問題 select file from file where name = 'test' or 1=1?
2.?使用PreparedStatement的set方法設置參數
String param = "'test' or 1=1"; String sql = "select file from file where name = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, param); ResultSet resultSet = preparedStatement.executeQuery(); System.out.println(resultSet.next());輸出結果為false,DB中執行的SQL為
select file from file where name = '\'test\' or 1=1'我們可以看到輸出的SQL文是把整個參數用引號包起來,并把參數中的引號作為轉義字符,從而避免了參數也作為條件的一部分
?
?
接下來我們分析下源碼(以mysql驅動實現為例)
打開java.sql.PreparedStatement通用接口,看到如下注釋,了解到PreparedStatement就是為了提高statement(包括SQL,存儲過程等)執行的效率。
An object that represents a precompiled SQL statement. A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.那么,什么是所謂的“precompiled SQL statement”呢?
回答這個問題之前需要先了解下一個SQL文在DB中執行的具體步驟:
而所謂的“precompiled SQL statement”,就是同樣的SQL文(包括不同參數的),1-4步驟只在第一次執行,所以大大提高了執行效率(特別是對于需要重復執行同一SQL的)
?
言歸正傳,回到source中,我們重點關注一下setString方法(因為其它設置參數的方法諸如setInt,setDouble之類,編譯器會檢查參數類型,已經避免了SQL注入。)
查看mysql中實現PreparedStatement接口的類com.mysql.jdbc.PreparedStatement中的setString方法(部分代碼)
public void setString(int parameterIndex, String x) throws SQLException {synchronized (checkClosed().getConnectionMutex()) {// if the passed string is null, then set this column to nullif (x == null) {setNull(parameterIndex, Types.CHAR);} else {checkClosed();int stringLength = x.length();if (this.connection.isNoBackslashEscapesSet()) {// Scan for any nasty chars// 判斷是否需要轉義處理(比如包含引號,換行等字符)boolean needsHexEscape = isEscapeNeededForString(x, stringLength); // 如果不需要轉義,則在兩邊加上單引號if (!needsHexEscape) {byte[] parameterAsBytes = null;StringBuilder quotedString = new StringBuilder(x.length() + 2);quotedString.append('\'');quotedString.append(x);quotedString.append('\'');...} else {...}String parameterAsString = x;boolean needsQuoted = true;// 如果需要轉義,則做轉義處理if (this.isLoadDataQuery || isEscapeNeededForString(x, stringLength)) {...從上面加紅色注釋的可以明白為什么參數會被單引號包裹,并且類似單引號之類的特殊字符會被轉義處理,就是因為這些代碼的控制避免了SQL注入。?
這里只對SQL注入相關的代碼進行解讀,如果在setString前后輸出預處理語句(preparedStatement.toString()),會發現如下輸出
Before bind: com.mysql.jdbc.JDBC42PreparedStatement@b1a58a3: select file from file where name = ** NOT SPECIFIED ** After bind: com.mysql.jdbc.JDBC42PreparedStatement@b1a58a3: select file from file where name = '\'test\' or 1=1'編程中建議大家使用PrepareStatement + Bind-variable的方式避免SQL注入
大家有什么其它的看法,歡迎留下評論!
參考:https://stackoverflow.com/questions/30587736/what-is-pre-compiled-sql-statement
總結
以上是生活随笔為你收集整理的【转】从源码分析PreparedStatement是如何防止SQL注入的?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想笔记本蓝屏怎么修复软件 联想笔记本蓝
- 下一篇: MyEclipse部署Web项目Serv