当前位置:网站首页>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的使用知识点,不然的话会被恶意登录。
边栏推荐
- drools决策表的简单使用
- WSL 2 will not be installed yet? It's enough to read this article
- AI mid stage technology research
- Leetcode209 subarray with the smallest length
- CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
- String palindrome hash template question o (1) judge whether the string is palindrome
- Drools executes string rules or executes a rule file
- Go learning notes - go based interprocess communication
- Go学习笔记—基于Go的进程间通信
- Calculate the maximum path sum of binary tree
猜你喜欢

MySQL and PostgreSQL methods to grab slow SQL

Sparkcontext: error initializing sparkcontext solution

Differences between nodes and sharding in ES cluster

CDA data analysis -- Introduction and use of aarrr growth model

Record the range of data that MySQL update will lock

Simple understanding of ThreadLocal

There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes

Jenkins user rights management

Jenkins voucher management

趣味 面试题
随机推荐
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
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
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
How to write a pleasing English mathematical paper
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
浏览器node事件循环
BOM DOM
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
CDA data analysis -- common knowledge points induction of Excel data processing
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Redis avalanche, penetration, breakdown
Leetcode209 subarray with the smallest length
drools执行String规则或执行某个规则文件
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
CDA数据分析——AARRR增长模型的介绍、使用
CDA data analysis -- Introduction and use of aarrr growth model
Sparkcontext: error initializing sparkcontext solution
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution