久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      了解SQL注入及如何解決

      了解SQL注入及如何解決

      推薦(免費):sql教程

      SQL注入即是指web應(yīng)用程序?qū)τ脩糨斎霐?shù)據(jù)的合法性沒有判斷或過濾不嚴,攻擊者可以在web應(yīng)用程序中事先定義好的查詢語句的結(jié)尾上添加額外的SQL語句,在管理員不知情的情況下實現(xiàn)非法操作,以此來實現(xiàn)欺騙數(shù)據(jù)庫服務(wù)器執(zhí)行非授權(quán)的任意查詢,從而進一步得到相應(yīng)的數(shù)據(jù)信息。

      1、SQL注入案例

      模擬一個用戶登錄的SQL注入案例,用戶在控制臺上輸入用戶名和密碼, 然后使用 Statement 字符串拼接的方式實現(xiàn)用戶的登錄。

      1.1 數(shù)據(jù)庫中先創(chuàng)建用戶表及數(shù)據(jù)

      -- 創(chuàng)建一張用戶表 CREATE TABLE `users` (   `id` INT(11) NOT NULL AUTO_INCREMENT,   `username` VARCHAR(20),   `password` VARCHAR(50),   PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8;   -- 插入數(shù)據(jù) INSERT INTO  users(username,`password`) VALUES('張飛','123321'),('趙云','qazxsw'),('諸葛亮','123Qwe'); INSERT INTO  users(username,`password`) VALUES('曹操','741258'),('劉備','plmokn'),('孫權(quán)','!@#$%^');   -- 查看數(shù)據(jù) SELECT  * FROM users;

      了解SQL注入及如何解決

      1.2 編寫一個登錄程序

      import java.sql.*; import java.util.Scanner;   public class TestSQLIn {     public static void main(String[] args) throws ClassNotFoundException, SQLException {         Class.forName("com.mysql.jdbc.Driver");         String url = "jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=UTF-8";         Connection conn = DriverManager.getConnection(url,"root","123456");         //System.out.println(conn);         // 獲取語句執(zhí)行平臺對象 Statement         Statement smt = conn.createStatement();           Scanner sc = new Scanner(System.in);         System.out.println("請輸入用戶名:");         String userName = sc.nextLine();         System.out.println("請輸入密碼:");         String password = sc.nextLine();           String sql = "select  * from users where username = '" + userName + "'  and  password = '" + password +"'";         //打印出SQL         System.out.println(sql);         ResultSet resultSet = smt.executeQuery(sql);         if(resultSet.next()){             System.out.println("登錄成功?。。?quot;);         }else{             System.out.println("用戶名或密碼錯誤,請重新輸入!??!");         }           resultSet.close();         smt.close();         conn.close();       }   }

      1.3 正常登錄

      輸入正確的用戶名及密碼后提示"登錄成功"

      了解SQL注入及如何解決

      1.4 登錄失敗

      輸入用戶名或密碼錯誤時,提示“用戶名或密碼錯誤,請重新輸入”

      了解SQL注入及如何解決

      1.5 模擬SQL注入

      拼接的字符串中有or '1'='1' 為恒成立條件,因此 及時前面的用戶及密碼不存在也會取出所有記錄,因此提示"登錄成功"

      了解SQL注入及如何解決

      1.6 SQL語法報錯

      使用拼接的方式,還會出現(xiàn)SQL語法錯誤等報錯,例如

      了解SQL注入及如何解決

      2. 解決方案

      使用Statement方式,用戶可以通過字符串拼接,改變原本SQL真正的含義,導(dǎo)致存在SQL注入的風險。解決SQL注入,可以通過預(yù)處理對象PreparedStatement來代替Statement進行處理。

      1.1 編寫一個新程序

      import java.sql.*; import java.util.Scanner;   public class TestSQLIn {     public static void main(String[] args) throws ClassNotFoundException, SQLException {         Class.forName("com.mysql.jdbc.Driver");         String url = "jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=UTF-8";         Connection conn = DriverManager.getConnection(url,"root","123456");         //System.out.println(conn);         // 獲取語句執(zhí)行平臺對象 Statement         // Statement smt = conn.createStatement();           Scanner sc = new Scanner(System.in);         System.out.println("請輸入用戶名:");         String userName = sc.nextLine();         System.out.println("請輸入密碼:");         String password = sc.nextLine();           String sql = "select  * from users where username = ? and  password = ? ";         // System.out.println(sql);         // ResultSet resultSet = smt.executeQuery(sql);         PreparedStatement preparedStatement = conn.prepareStatement(sql);         preparedStatement.setString(1,userName);         preparedStatement.setString(2,password);           ResultSet  resultSet = preparedStatement.executeQuery();         if(resultSet.next()){             System.out.println("登錄成功?。?!");         }else{             System.out.println("用戶名或密碼錯誤,請重新輸入?。?!");         }             preparedStatement.close();         resultSet.close();         // smt.close();         conn.close();       }   }

      2.2 正常登錄

      了解SQL注入及如何解決

      2.3 用戶名密碼錯誤

      當用戶名或密碼輸入錯誤時,會提示“用戶名或密碼錯誤,請重新輸入”

      了解SQL注入及如何解決

      2.4 模擬SQL注入

      按照之前的情況,進行SQL注入的寫法,測試后不再出現(xiàn)SQL注入情況。

      了解SQL注入及如何解決

      2.5 模擬SQL語法錯誤

      使用預(yù)處理類后,輸入帶有單引號或雙引號的內(nèi)容也不會再出現(xiàn)SQL語法錯誤的報錯

      了解SQL注入及如何解決

      3. 小結(jié)

      Statement 與 PreparedStatement的主要區(qū)別如下:

      • Statement用于執(zhí)行靜態(tài)SQL語句,在執(zhí)行時,必須指定一個事先準備好的SQL語句

      • PrepareStatement是預(yù)編譯的SQL語句對象,語句中可以包含動態(tài)參數(shù)“?”,在執(zhí)行時可以為“?”動態(tài)設(shè)置參數(shù)值

      • PrepareStatement可以減少編譯次數(shù)提高數(shù)據(jù)庫性能

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號