Mybaits笔记
有关sql注入和mybatis解决
mybatis使用 #{} 占位符预编译的方式解决sql注入
mysql执行sql语句有四个步骤
缓存部分:sql语法解析->优化sql->编译sql —> 执行sql
在Java往数据库传入数据是如果使用预编译:
预编译代码示例:
select count(* ) from user where username = #{} and password = #{};
在缓存中存的sql语句为:
select count(*) from user where username = ? and password = ?
执行预编译代码只需要向username和password传入参数
不使用预编译:
不适用预编译代码示例:
select count(*) from user where username = ’ ’ and password =’ ';
sql注入:
账号随便填写:admin
密码框写sql语句: ’ or ‘1’ = '1
数据库执行sql语句为 select count(*) from user where username = ’ ’ and password =’ ’ or ‘1’ = '1 ’;
由于 1 = 1 为true所以可以直接登录访问
该博客围绕MyBatis笔记展开,重点介绍了SQL注入及MyBatis的解决办法。MyBatis通过使用#{}占位符预编译方式解决SQL注入,还给出了预编译和不使用预编译的代码示例,同时说明了不使用预编译时可能出现的SQL注入情况。

878





