SQL注入就是通过把SQL命令插入到web表单提交, 或者输入到域名/页面请求的查询字符串中, 最终使服务执行恶意的SQL命令
正常输入用户名密码, 后端执行的SQL语句 用户名:python 密码:123456
select * from users where username = 'python' and password = '123456'
SQL注入语句, 后端执行的SQL语句 用户名: 'or 1 = 1 密码随便输入
select * from users where username = '' or 1 = 1# ' and password = ''
# 是mysql中的注释符号, - 是oracle的注释符号 注释符号后的代码不会被执行
等价于
select * from users where username = '' or 1 = 1
1 = 1 为 True , 这样就可以绕过sql验证, 所以就能搜索到表中所有相关数据
防止SQL注入
不要信任用户的输入, 对用户的输入进行校验
- 使用正则对数据进行判断判断
- 对数据长度进行限制
- 对单引号和双引号进行转换
不使用动态拼装SQL
对用户的机密信息进行加密保存