溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

出現(xiàn)SQL注入如何解決

發(fā)布時間:2021-01-05 14:58:29 來源:億速云 閱讀:124 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了出現(xiàn)SQL注入如何解決,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

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;

出現(xiàn)SQL注入如何解決

1.2 編寫一個登錄程序

package com.study.task0201;

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 正常登錄

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

出現(xiàn)SQL注入如何解決

1.4 登錄失敗

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

出現(xiàn)SQL注入如何解決

1.5 模擬SQL注入

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

出現(xiàn)SQL注入如何解決

1.6 SQL語法報錯

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

出現(xiàn)SQL注入如何解決

2. 解決方案

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

2.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("用戶名或密碼錯誤,請重新輸入!?。?quot;);
  }


  preparedStatement.close();
  resultSet.close();
  // smt.close();
  conn.close();

 }

}

2.2 正常登錄

出現(xiàn)SQL注入如何解決

2.3 用戶名密碼錯誤

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

出現(xiàn)SQL注入如何解決

2.4 模擬SQL注入

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

出現(xiàn)SQL注入如何解決

2.5 模擬SQL語法錯誤

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

出現(xiàn)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ù)庫性能

上述內(nèi)容就是出現(xiàn)SQL注入如何解決,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

sql
AI