0

【Mybatis】-防止SQL注入

 1 month ago
source link: https://blog.51cto.com/u_15858858/10245561
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在application.properties中,打开mybatis的日志,并指定输出到控制台

#配置日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
【Mybatis】-防止SQL注入_预编译
【Mybatis】-防止SQL注入_预编译_02

为何采用预编译SQL

更安全(防止SQL注入)

这是通过java来操控SQL语句的流程,为了提高效率,数据库服务器会将编译后的SQL缓存起来,当下一次再执行SQL语句的时候,它会先检查缓存,看一下缓存当中是否有编译好的SQL语句,如果有,就不用执行前三部分了

【Mybatis】-防止SQL注入_SQL_03

当没有预编译时,执行下面三条语句会执行三次整个过程,就只是因为id的值不一样

【Mybatis】-防止SQL注入_缓存_04

当我们采用预编译时,我们就不用再执行整个流程了,只需改变id值,直接执行SQL语句

【Mybatis】-防止SQL注入_缓存_05
【Mybatis】-防止SQL注入_SQL_06

SQL注入

SQL注入是通过操作输入的数据来修改事先定义好的SQL语句,以达到执行代码对服务器进行公鸡(敏感文字,哈哈哈哈,手动狗头)的方法。

我们来演示一下SQL注入

-- 登录 如果count(*)大于0正确,等于0错误
select count(*) from emp where username ='zhangwuji' and password = '123456';
【Mybatis】-防止SQL注入_SQL_07

用户名和密码正确

【Mybatis】-防止SQL注入_缓存_08
【Mybatis】-防止SQL注入_预编译_09

现在我们来SQL注入,发现也登录进去了

【Mybatis】-防止SQL注入_缓存_10
【Mybatis】-防止SQL注入_预编译_11
select count(*) from emp where username ='weurfidjs' and password = '' or '1' = '1';
【Mybatis】-防止SQL注入_预编译_12

预处理就能解决这个问题

【Mybatis】-防止SQL注入_缓存_13

参数占位符也有两种

【Mybatis】-防止SQL注入_预编译_14

$符有风险

【Mybatis】-防止SQL注入_SQL_15

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK