当前位置:网站首页>JDBC prevent SQL injection problems and solutions [preparedstatement]
JDBC prevent SQL injection problems and solutions [preparedstatement]
2022-07-02 12:42:00 【Attitude still needs efforts】
Preface
To demonstrate prevention sql Injection problem , We use user login to enter user name and password to explain and solve problems .
The database table is shown in the figure :
One 、 Problem explanation
First use the commonly used Statement
String name="tom";
// String psw="123456";
String psw="' or '1'='1";
// Before solving the problem
// 3. Definition sql
String sql="SELECT * FROM user_info where name='"+name+"' and password='"+psw+"'";
Statement stmt= (Statement) conn.createStatement();
//5. perform sql
ResultSet rs = stmt.executeQuery(sql);
Login judgment code
if(rs.next()){
System.out.println(" Landing successful ~");
}else{
System.out.println(" Login failed ~");
}
If the password is ' or '1'='1 , Execution results
We will find that , Database table tom Your password is not ' or '1'='1, But you can login successfully ! Why is that ?
Let's print sql Statement analysis reason
System.out.println(sql);
Running results
Here we can see where Conditions :name=‘tom’ It's true password=‘’ For false ‘1’=‘1’ It's true .
Can be judged really and false or really =( false or really )= really . The result is always true , So you can log in successfully !
But how to solve this problem !!! If you use this, you can log in to someone else's account at will .
Two 、 resolvent
We just need to use PreparedStatement solve Escape sensitive characters
Code :
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();
Now the running result
Use here The password for ' or '1'='1 Login success will not be displayed . Why is that ?
Look at the above first sql Output select * from user_info where name=? and password=?
It first escapes sensitive characters
hold ' or '1'='1 Escape character \' or \'1\'=\'1 Into text form therefore sql Statement will not find the result
That's it sql Injection problem
summary
This is also a problem that needs to be paid attention to when you do your own projects in the later stage ,PreparedStatement The use of knowledge points , Otherwise, you will be logged in maliciously .
边栏推荐
- Anti shake throttle
- 单指令多数据SIMD的SSE/AVX指令集和API
- Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- Redis avalanche, penetration, breakdown
- 应用LNK306GN-TL 转换器、非隔离电源
- JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- ArrayList与LinkedList效率的对比
- Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
猜你喜欢
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
染色法判定二分图 AcWing 860. 染色法判定二分图
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
Use sqoop to export ads layer data to MySQL
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
[FFH] little bear driver calling process (take calling LED light driver as an example)
C#修饰符
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
随机推荐
线性DP AcWing 899. 编辑距离
Docker compose configuration mysql, redis, mongodb
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
Leetcode - Sword finger offer 59 - I, 59 - II
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
Oracle从入门到精通(第4版)
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Shutter encapsulated button
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
[ybtoj advanced training guide] similar string [string] [simulation]
百款拿来就能用的网页特效,不来看看吗?
线性DP AcWing 902. 最短编辑距离
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
深拷贝 事件总线
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
String palindrome hash template question o (1) judge whether the string is palindrome
Initial JDBC programming
防抖 节流
Go learning notes - go based interprocess communication