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来进行预处理。
本文由博客一文多发平台 OpenWrite 发布!
博主邮箱:liunaijie1996@163.com,有问题可以邮箱交流。
Mybatis是如何实现SQL防注入的的更多相关文章
- SQL防注入程序 v1.0
/// ***************C#版SQL防注入程序 v1.0************ /// *使用方法: /// 一.整站防注入(推荐) /// 在Global.asax.cs中查找App ...
- PHP之SQL防注入代码集合(建站常用)
SQL防注入代码一 <?php if (!function_exists (quote)) { function quote($var) { if (strlen($var)) { $var=! ...
- sql 防注入 维基百科
http://zh.wikipedia.org/wiki/SQL%E8%B3%87%E6%96%99%E9%9A%B1%E7%A2%BC%E6%94%BB%E6%93%8A SQL攻击(SQL inj ...
- 特殊字符的过滤方法,防sql防注入代码的过滤方法
特殊字符的过滤方法 function strFilter($str){ //特殊字符的过滤方法 $str = str_replace('`', '', $str); $str = str_replac ...
- SQL防注入程序
1.在Global.asax.cs中写入: protected void Application_BeginRequest(Object sender,EventArgs e){ SqlIn ...
- PHP SQL防注入
过年前后在做一个抽奖的东西,需要用户填写中奖信息,为了防止非法用户对数据库进行入侵神马的,于是写下基本的防注入语句,需要用的可以自己封装成一个function. $str = str_replace( ...
- 360提供的SQL防注入
<?php class sqlsafe { private $getfilter = "'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\ ...
- .net sql 防注入 httpmodule
1 新建一个类,实现IHttpModule接口 using System; using System.Collections.Generic; using System.Linq; using Sys ...
- PHP之SQL防注入代码(360提供)
<?php class sqlsafe { private $getfilter = "'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\ ...
随机推荐
- ORA-01089: 即時シャットダウン処理中 - 操作はできません
一:当时的情景 SQL> shutdown immediate --无任何返回结果 二:问题定位过程 1.查询相关进程只有ORACLE的关键进程存在 ps -ef |grep ora_ soad ...
- iptables禁止强制访问指定的站点
要使192.168.52.0/24网络内的计算机(这此计算机的网关应设为192.168.52.10)强制访问指定的站点,在做为防火墙的计算机(192.168.52.10)上应添加以下规则: 1. 打开 ...
- C#的类
一.String类 1.Length 字符的长度 string x = Console.ReadLine();int i = x.Length;// Length 是获取字符串的长度(从1开始数)Co ...
- Codeforces Round #167 (Div. 1 + Div. 2)
C. Dima and Staircase 线段树维护区间最大值. D. Dima and Two Sequences 由于模数不一定为质数,所以通过拆分质因数来做阶乘取模. E. Dima and ...
- RBF神经网络通用函数 newrb, newrbe
RBF神经网络通用函数 newrb, newrbe 1.newrb 其中P为输入向量,T为输出向量,GOAL为均方误差的目标,SPREED为径向基的扩展速度.返回值是一个构建好的网络,用newrb ...
- 用diiv实现多个方块居中嵌套--margin
文章地址 https://www.cnblogs.com/sandraryan/ 案例:用diiv嵌套多个正方形,配合盒模型相关知识,使每个div在他的父元素上居中.(每个div中心点对齐) 涉及到m ...
- Python--day24--继承
A_son.__bases__查看继承的父类是哪些 A. object是所有类的祖宗,所有的类都默认继承了object类. python中可以多继承 继承与抽象,先抽象再继承: example:
- Tensorflow安装问题: Could not find a version that satisfies the requirement tensorflow pip命令
引言: Tensorflow大名鼎鼎,这里不再赘述其为何物.这里讲描述在安装python包的时候碰到的“No matching distribution found for tensorflow”,其 ...
- java线程与进程的比较
线程具有许多传统进程所具有的特征,故又称为轻型进程(Light—Weight Process)或进程元:而把传统的进程称为重型进程(Heavy—Weight Process),它相当于只有一个线程的任 ...
- 【t088】倒水
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 一天辰辰买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着辰辰发现瓶子实在太多了,于是 ...