当前位置:网站首页>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的使用知识点,不然的话会被恶意登录。
边栏推荐
- [FFH] little bear driver calling process (take calling LED light driver as an example)
- async/await 异步函数
- Record the range of data that MySQL update will lock
- [C language] convert decimal numbers to binary numbers
- drools执行完某个规则后终止别的规则执行
- drools执行String规则或执行某个规则文件
- Drools dynamically add, modify, and delete rules
- In development, why do you find someone who is paid more than you but doesn't write any code?
- 浏览器node事件循环
- 线性DP AcWing 902. 最短编辑距离
猜你喜欢

(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.

染色法判定二分图 AcWing 860. 染色法判定二分图

区间DP AcWing 282. 石子合并

Go learning notes - multithreading

(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?

计数类DP AcWing 900. 整数划分

线性DP AcWing 898. 数字三角形

MySQL indexes and transactions

Embedded Software Engineer career planning
![[ybtoj advanced training guidance] judgment overflow [error]](/img/be/bbe357ac2f2a8839afc5af47db88d0.jpg)
[ybtoj advanced training guidance] judgment overflow [error]
随机推荐
LeetCode—剑指 Offer 37、38
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
Distributed machine learning framework and high-dimensional real-time recommendation system
Docker compose configuration mysql, redis, mongodb
Sweetheart leader: Wang Xinling
怎样写一篇赏心悦目的英文数学论文
寻找二叉树中任意两个数的公共祖先
模块化 CommonJS ES Module
Openssh remote enumeration username vulnerability (cve-2018-15473)
VLAN experiment
drools执行完某个规则后终止别的规则执行
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
Bom Dom
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Redis avalanche, penetration, breakdown
In development, why do you find someone who is paid more than you but doesn't write any code?
Multiply LCA (nearest common ancestor)
Rust search server, rust quick service finding tutorial