JDBC模擬登錄及SQL語句防注入問題

實現模擬登錄效果:基於表Tencent
java

 1 package boom;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 import java.util.Date;
 9 import java.util.Scanner;
10 
11 /**
12  * 模擬登錄
13  * 
14  * @author Administrator
15  *
16  */
17 public class JdbcLogin {
18     public static void main(String[] args) {
19         // 接收控制檯信息
20         Scanner scanner = new Scanner(System.in);
21         // 提示語句
22         System.out.println("請輸入用戶名:");
23         String name = scanner.next();
24         System.out.println("請輸入密碼:");
25         String pwd = scanner.next();
26 
27         // 聲明參數
28         String driver = "oracle.jdbc.driver.OracleDriver";
29         String url = "jdbc:oracle:thin:@localhost:1521:XE";
30         String userName = "scott";
31         String userPwd = "tiger";
32 
33         // 聲明鏈接,初始化爲空
34         Connection connection = null;
35         Statement statement = null;
36         ResultSet resultSet = null;
37 
38         try {
39             // 1.加載驅動
40             Class.forName(driver);
41             // 2.建立鏈接
42             connection = DriverManager.getConnection(url, userName, userPwd);
43             // 3.建立SQL命令發送器
44             statement = connection.createStatement();
45             // 4.發送SQL獲取結果
46             // 查詢表裏的對應數據源
47             String sql = "select * from Tencent where uname = '"+name+"' and upwd = '"+pwd+"' ";
48             System.out.println("HelloJdbcLogin.main(sql):"+sql);
49             resultSet = statement.executeQuery(sql);
50             //5:處理結果--判斷是否有這個用戶
51             if(resultSet.next()){
52                 System.out.println("HelloJdbcLogin.main(登錄成功)");
53             }else{
54                 System.out.println("HelloJdbcLogin.main(登錄失敗)");
55             }
56             
57         } catch (ClassNotFoundException e) {
58             // TODO Auto-generated catch block
59             e.printStackTrace();
60         } catch (SQLException e) {
61             // TODO Auto-generated catch block
62             e.printStackTrace();
63         } finally {
64             // 6.關閉資源 【先開後關】
65             try {
66                 if (resultSet != null) {
67                     resultSet.close();
68                 }
69             } catch (SQLException e) {
70                 // TODO Auto-generated catch block
71                 e.printStackTrace();
72             }
73             try {
74                 if (statement != null) {
75                     statement.close();
76                 }
77             } catch (SQLException e) {
78                 // TODO Auto-generated catch block
79                 e.printStackTrace();
80             }
81             try {
82                 if (connection != null) {
83                     connection.close();
84                 }
85             } catch (SQLException e) {
86                 // TODO Auto-generated catch block
87                 e.printStackTrace();
88             }
89         }
90     }
91 
92 }
View Code

效果:sql

--請輸入用戶名:
迪麗熱巴
--請輸入密碼:
1213456
--HelloJdbcLogin.main(sql):select * from Tencent where uname = '迪麗熱巴' and upwd = '1213456' 
HelloJdbcLogin.main(登錄成功)
----------------------------------------------------------------------------------------------
--請輸入用戶名:
迪麗熱巴
--請輸入密碼:
12346
--HelloJdbcLogin.main(sql):select * from Tencent where uname = '迪麗熱巴' and upwd = '12346' 
HelloJdbcLogin.main(登錄失敗)

執行代碼用SQL注入拼接實現【SQL注入成功】安全

--請輸入用戶名:
小喜慶
--請輸入密碼:
664654'or'1'='1
--HelloJdbcLogin.main(sql):select * from Tencent where uname = '小喜慶' and upwd = '664654'or'1'='1' 
HelloJdbcLogin.main(登錄成功)

爲何會注入成功?
oracle

Statement:不安全,拼接麻煩,閱讀性差
PreparedStatement:安全,閱讀性好,(執行效率高)
防止SQL注入風險:PreparedStatementide

// 聲明鏈接,初始化爲空
Connection connection = null;
// 修改Statement ==> PreparedStatement
PreparedStatement ps = null;
ResultSet resultSet = null;

try {
	// 加載驅動
	Class.forName(driver);
	// 建立鏈接
	connection = DriverManager.getConnection(url, userName, userPwd);
	// 建立SQL命令發送器
	// ? 至關於佔位符
	String sql = "select * from Tencent where uname = ? and upwd =?";
	ps=connection.prepareStatement(sql);
	// 賦值佔位
	ps.setString(1, name);
	ps.setString(2, pwd);
	// 4.發送SQL獲取結果
	System.out.println("HelloJdbcLogin.main(sql):"+sql);
	resultSet = ps.executeQuery();
	//5:處理結果--判斷是否有這個用戶
	if(resultSet.next()) {
		System.out.println("HelloJdbcLogin.main(登錄成功)");
	} else {
		System.out.println("HelloJdbcLogin.main(登錄失敗)");
	}

詳細代碼:url

 1 package boom;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import java.util.Scanner;
 9 
10 public class JdbcLogin2 {
11     public static void main(String[] args) {
12         // 接收控制檯信息
13         Scanner scanner = new Scanner(System.in);
14         // 提示語句
15         System.out.println("請輸入用戶名:");
16         String name = scanner.next();
17         System.out.println("請輸入密碼:");
18         String pwd = scanner.next();
19 
20         // 聲明參數
21         String driver = "oracle.jdbc.driver.OracleDriver";
22         String url = "jdbc:oracle:thin:@localhost:1521:XE";
23         String userName = "scott";
24         String userPwd = "tiger";
25 
26         // 聲明鏈接,初始化爲空
27         Connection connection = null;
28         PreparedStatement ps = null;
29         ResultSet resultSet = null;
30 
31         try {
32             // 1.加載驅動
33             Class.forName(driver);
34             // 2.建立鏈接
35             connection = DriverManager.getConnection(url, userName, userPwd);
36             // 3.建立SQL命令發送器
37             //?至關於佔位符
38             String sql = "select * from Tencent where uname = ? and upwd =?";
39             ps=connection.prepareStatement(sql);
40             // 賦值
41             ps.setString(1, name);
42             ps.setString(2, pwd);
43             // 4.發送SQL獲取結果
44             System.out.println("HelloJdbcLogin.main(sql):"+sql);
45             resultSet = ps.executeQuery();
46             //5:處理結果--判斷是否有這個用戶
47             if(resultSet.next()){
48                 System.out.println("HelloJdbcLogin.main(登錄成功)");
49             }else{
50                 System.out.println("HelloJdbcLogin.main(登錄失敗)");
51             }
52             
53         } catch (ClassNotFoundException e) {
54             // TODO Auto-generated catch block
55             e.printStackTrace();
56         } catch (SQLException e) {
57             // TODO Auto-generated catch block
58             e.printStackTrace();
59         } finally {
60             // 6.關閉資源 【先開後關】
61             try {
62                 if (resultSet != null) {
63                     resultSet.close();
64                 }
65             } catch (SQLException e) {
66                 // TODO Auto-generated catch block
67                 e.printStackTrace();
68             }
69             try {
70                 if (ps != null) {
71                     ps.close();
72                 }
73             } catch (SQLException e) {
74                 // TODO Auto-generated catch block
75                 e.printStackTrace();
76             }
77             try {
78                 if (connection != null) {
79                     connection.close();
80                 }
81             } catch (SQLException e) {
82                 // TODO Auto-generated catch block
83                 e.printStackTrace();
84             }
85         }
86     }
87 
88 }
View Code

【SQL注入失敗】spa

--請輸入用戶名:
小喜慶
--請輸入密碼:
664654'or'1'='1
--HelloJdbcLogin.main(sql):select * from Tencent where uname = ? and upwd =?
HelloJdbcLogin.main(登錄失敗)
相關文章
相關標籤/搜索