当前位置:网站首页>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 .
边栏推荐
- JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
- 模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
- 通过反射执行任意类的任意方法
- 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
- 基于STM32的OLED 屏幕驱动
- 模块化 CommonJS ES Module
- Is the neural network (pinn) with embedded physical knowledge a pit?
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
- spfa AcWing 852. spfa判断负环
猜你喜欢

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

Dijkstra AcWing 850. Dijkstra求最短路 II

arcgis js 4. Add pictures to x map

Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)

AI中台技术调研

高性能纠删码编码

Find the common ancestor of any two numbers in a binary tree

模块化 CommonJS ES Module

Sparkcontext: error initializing sparkcontext solution

Record the range of data that MySQL update will lock
随机推荐
深拷貝 事件總線
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
应用LNK306GN-TL 转换器、非隔离电源
ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area
模块化 CommonJS ES Module
Leetcode - Sword finger offer 59 - I, 59 - II
Use MySQL events to regularly perform post seven world line tasks
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
Redis bloom filter
Multiply LCA (nearest common ancestor)
Calculate the maximum path sum of binary tree
8A 同步降压稳压器 TPS568230RJER_规格信息
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Simple use of drools decision table
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
[FFH] little bear driver calling process (take calling LED light driver as an example)
H5 to app
How to write a pleasing English mathematical paper