当前位置:网站首页>JDBC 预防sql注入问题与解决方法[PreparedStatement]
JDBC 预防sql注入问题与解决方法[PreparedStatement]
2022-07-02 09:43:00 【心态还需努力呀】
前言
为演示预防sql注入问题,我们使用用户登录输入用户名和密码来说明问题和解决问题。
数据库表如图:

一、问题解释
先用常用的Statement
String name="tom";
// String psw="123456";
String psw="' or '1'='1";
// 解决问题前
// 3.定义sql
String sql="SELECT * FROM user_info where name='"+name+"' and password='"+psw+"'";
Statement stmt= (Statement) conn.createStatement();
//5.执行sql
ResultSet rs = stmt.executeQuery(sql);登录判断代码
if(rs.next()){
System.out.println("登陆成功~");
}else{
System.out.println("登陆失败~");
}如果密码为' or '1'='1 ,执行结果

我们会发现,数据库表tom的密码不是 ' or '1'='1,但可以登录成功!这是为什么?
我们打印下sql语句分析原因
System.out.println(sql);运行结果
这里我们可以看where条件:name=‘tom’为真 password=‘’为假 ‘1’=‘1’为真。
可以判断 真 and 假 or 真 =(假 or 真)=真。结果永远为真,所以可以登录成功!
但怎么解决这个问题呢!!!如果这用都可以随意登录别人的账号了。
二、解决方法
我们只需要使用PreparedStatement解决 将敏感字符进行转义
代码:
String sql="select * from user_info where name=? and password=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2,psw);
ResultSet rs=pstmt.executeQuery();现在的运行结果
这里使用密码为' or '1'='1 就不会显示登录成功。这是为什么呢?
先看上述sql输出的select * from user_info where name=? and password=?
它是先将敏感字符进行转义
把' or '1'='1 转义字符\' or \'1\'=\'1 变成了文本形式 所以sql语句会找不到结果
这样就解决了sql注入的问题
总结
这个也是后期自己做项目时需要注意的问题,PreparedStatement的使用知识点,不然的话会被恶意登录。
边栏推荐
- The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
- Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
- Redis avalanche, penetration, breakdown
- Sse/avx instruction set and API of SIMD
- 深拷贝 事件总线
- js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
- LeetCode—剑指 Offer 59 - I、59 - II
- What data types does redis have and their application scenarios
- 记录一下MySql update会锁定哪些范围的数据
- Gaode map test case
猜你喜欢

Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students

线性DP AcWing 899. 编辑距离

High performance erasure code coding

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime

arcgis js 4. Add pictures to x map
![[ybtoj advanced training guidance] judgment overflow [error]](/img/be/bbe357ac2f2a8839afc5af47db88d0.jpg)
[ybtoj advanced training guidance] judgment overflow [error]

Addition, deletion, modification and query of MySQL table (Advanced)

In development, why do you find someone who is paid more than you but doesn't write any code?

Deep Copy Event bus

Is the neural network (pinn) with embedded physical knowledge a pit?
随机推荐
Go学习笔记—基于Go的进程间通信
VLAN experiment
趣味 面试题
SparkContext: Error initializing SparkContext解决方法
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
[ybtoj advanced training guidance] judgment overflow [error]
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
drools执行指定的规则
"As a junior college student, I found out how difficult it is to counter attack after graduation."
Enhance network security of kubernetes with cilium
Leetcode209 subarray with the smallest length
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
Map和Set
Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
MySQL indexes and transactions
mysql数据库基础
drools中then部分的写法
染色法判定二分图 AcWing 860. 染色法判定二分图