当前位置:网站首页>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的使用知识点,不然的话会被恶意登录。
边栏推荐
- [ybtoj advanced training guide] similar string [string] [simulation]
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- Leetcode122 the best time to buy and sell stocks II
- 甜心教主:王心凌
- 高性能纠删码编码
- In development, why do you find someone who is paid more than you but doesn't write any code?
- BOM DOM
- Post request body content cannot be retrieved repeatedly
- Input box assembly of the shutter package
- Use sqoop to export ads layer data to MySQL
猜你喜欢
![2.7 binary tree, post order traversal - [FBI tree]](/img/6b/1ded3632cc69329d7b2762ce47fdbc.jpg)
2.7 binary tree, post order traversal - [FBI tree]

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

ThreadLocal的简单理解

Drools dynamically add, modify, and delete rules

Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution

Brush questions --- binary tree --2

Sort---

Distributed machine learning framework and high-dimensional real-time recommendation system

高性能纠删码编码

Adding database driver to sqoop of cdh6
随机推荐
[ybtoj advanced training guidance] judgment overflow [error]
MySQL indexes and transactions
分布式机器学习框架与高维实时推荐系统
Writing method of then part in drools
VLAN experiment
Tas (file d'attente prioritaire)
Leetcode739 daily temperature
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
Is the neural network (pinn) with embedded physical knowledge a pit?
Leetcode - Sword finger offer 37, 38
怎样写一篇赏心悦目的英文数学论文
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Enhance network security of kubernetes with cilium
Record the range of data that MySQL update will lock
CDA数据分析——Excel数据处理的常见知识点归纳
Leetcode122 the best time to buy and sell stocks II
寻找二叉树中任意两个数的公共祖先
Deep understanding of P-R curve, ROC and AUC
深拷贝 事件总线
堆(優先級隊列)