风也温柔

计算机科学知识库

为什么在Java中能够有效防止SQL注入?这可能是每个Java程序员思考过的问题

  3A3R 模型:用户增长与留存利器

  学会大厂 Redis 缓存一致性设计

  大厂面试直通卡

  为什么Java可以有效防止SQL注入?这大概是每个Java程序员都思考过的问题。

  首先我们来看看直观的现象(注意:需要提前打开mysql的SQL日志)

  1.不要使用set方法设置参数(效果类似,相当于执行静态SQL)

  <pre>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());</pre>

  输出结果为真,DB中执行的SQL为

  <pre>-- 永真条件1=1成为了查询条件的一部分,可以返回所有数据,造成了SQL注入问题 select file from file where name = 'test' or 1=1 </pre>

  2.使用set方法设置参数

  <pre>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());</pre>

  输出结果为假,DB中执行的SQL为

  <pre>select file from file where name = ''test' or 1=1'</pre>

  我们可以看到输出的SQL文本将整个参数用引号括起来,并将参数中的引号用作转义字符,从而避免将参数作为条件的一部分

  接下来我们分析源码(以mysql驱动实现为例)

  打开 java.sql。通用接口,看下面的注释,明白是为了提高执行效率(包括SQL、存储过程等)。

  <pre>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.</pre>

  那么
为什么在Java中能够有效防止SQL注入?这可能是每个Java程序员思考过的问题,什么是所谓的“SQL”?

  在回答这个问题之前,你需要了解下一个SQL文本在DB中执行的具体步骤:

  给定SQL查询到DB——将SQL语句转化为DB形式(语法树结构) Check for——检查语法 Check for——检查语义计划——准备执行计划(也是一个优化过程,这一步比较重要,关系到你的SQL文本的效率,准备在后续文章中介绍)将运行时设置到查询中--设置运行时参数运行查询并获取--执行查询并获取结果

  所谓“SQL”就是同一个SQL文本(包括不同的参数),步骤1-4只是第一次执行,所以执行效率大大提高(特别是对于那些需要执行重复相同的SQL)

  言归正传,言归正传,我们来关注方法(因为其他设置参数的方法如 等java 防sql注入代码,编译器会检查参数类型,避免了SQL注入。)

  查看com.mysql.jdbc类中的方法。在mysql中实现接口(部分代码)

  <pre> 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)) {
            ...    </pre>

  从上面的红色注释可以理解为什么参数用单引号括起来java 防sql注入代码,而像单引号这样的特殊字符被转义了,因为这些代码的控制避免了SQL注入。

  这里只解释与 SQL 注入相关的代码。如果你在前后输出 (.()),你会发现如下输出

  <pre>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'</pre>

  在编程中,建议使用+Bind-来避免SQL注入

  如果您有其他意见,请发表评论!

  参考:

  文章来源:https://article.itxueyuan.com/Jkeew