温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Mybatis中怎么实现SQL防注入

发布时间:2021-07-24 15:37:01 来源:亿速云 阅读:145 作者:Leah 栏目:编程语言

这篇文章给大家介绍Mybatis中怎么实现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防注入就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI