为什么在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(, 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中执行的具体步骤:

  1. Convert given SQL query into DB format -- 将SQL语句转化为DB形式(语法树结构)
  2. Check for syntax -- 检查语法
  3. Check for semantics -- 检查语义
  4. Prepare execution plan -- 准备执行计划(也是优化的过程,这个步骤比较重要,关系到你SQL文的效率,准备在后续文章介绍)
  5. Set the run-time values into the query -- 设置运行时的参数
  6. Run the query and fetch the output -- 执行查询并取得结果

而所谓的“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 null
if (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

最新文章

  1. kvm 使用入门详解
  2. [Linux]Linux系统调用列表
  3. 通过Maven插件发布JaveEE项目到tomcat下
  4. Adb refused a command 解决方法
  5. SSH环境 jsp url跳转,带中文参数乱码问题
  6. VB的try语句,异常处理
  7. stm32f103 SPI单线TX发数据来驱动LCD
  8. Skeletal Animation
  9. IOS 生成设备唯一标识
  10. hdu 4710 Balls Rearrangement
  11. C指针--通过二级指针往回拉数据
  12. P2590 [ZJOI2008]树的统计
  13. [转帖]linux下的X server:linux图形界面原理
  14. 代替C++的getchar()
  15. css中:hover空格
  16. Summary: gcd最大公约数、lcm最小公倍数算法
  17. poj1456---贪心
  18. RTX——第9章 任务运行在特权级或非特权级模式
  19. C# 实现将listview中已经显示的数据导出到Access 数据库
  20. What is difference between 3-layer architecture and MVC architecture?

热门文章

  1. MyBatis总结七:动态sql和sql片段
  2. Android Notification通知
  3. CF702E Analysis of Pathes in Functional Graph
  4. 数据结构_just_sort
  5. C++的运算符重载 (转)
  6. HTML 4.0 触发事件
  7. springMVC:modelandview,model,controller,参数传递
  8. android加载字体内存泄漏的处理方法
  9. c# 汉字换英文,英文转汉字
  10. 适配器设计模式及GenericServlet(九)