解決 SQL 注入問題

解決 SQL 注入問題

推薦(免費):SQL教程

SQL注入是什么?

看一下百度百科的定義:
解決 SQL 注入問題
啊,好長一大段文字,些許不想看,下面通過一個例子,來說明一下什么是SQL注入:

新建一個數(shù)據(jù)庫,再建一個表,添加兩行數(shù)據(jù):

use db1;create table user( 	id int primary key auto_increment, 	username varchar(32), 	password varchar(32));insert into user values(null,'zhangsan','123');insert into user values(null,'lisi','234');

表如下圖所示:
解決 SQL 注入問題
再隨隨便便用JDBC寫個登陸操作:

package com.wzq.jdbc;import com.wzq.util.JDBCUtils;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Scanner;/*  *   需求:  *       1、通過鍵盤錄入用戶名和密碼  *       2、判斷用戶是否登陸成功  * */public class JDBCDemo05 {      public static void main(String[] args) {         Scanner cin = new Scanner(System.in);         System.out.println("請輸入用戶名:");         String username = cin.nextLine();         System.out.println("請輸入密碼:");         String password = cin.nextLine();          boolean res = new JDBCDemo05().login(username, password);         if (res) System.out.println("登陸成功!");         else System.out.println("登陸失敗!");      }      public boolean login(String username, String password) {         if (username == null || password == null) {             return false;         }         Connection conn = null;         Statement stmt = null;         ResultSet rs = null;         try {             //1、獲取數(shù)據(jù)庫連接             conn = JDBCUtils.getConnection();   //JDBCUtils工具類             //2、定義sql             String sql = "select * from user where username = '" + username + "' and password = '" + password + "'";             //3、獲取執(zhí)行sql的對象             stmt = conn.createStatement();             //4、執(zhí)行sql             rs = stmt.executeQuery(sql);             return rs.next();         } catch (SQLException e) {             e.printStackTrace();         } finally {             JDBCUtils.close(rs, stmt, conn);         }         return false;     }}

測試一下:
解決 SQL 注入問題
可以看到,普通的檢驗沒有任何問題,現(xiàn)在使用SQL注入:

賬戶名稱隨便輸入,密碼輸入:a’ or ‘a’=’a
解決 SQL 注入問題
驚訝的發(fā)現(xiàn),居然登陸成功了。輸出一下sql看一下:

select * from user where username = 'askjdhjksahd' and password = 'a' or 'a' = 'a'

可以看到where之后的條件,無論是什么結(jié)果都為真,都會輸出整個表:
解決 SQL 注入問題
所以,綜上所述:在sql拼接時,有一些sql的特殊關(guān)鍵字參與字符串的拼接,就會造成安全性問題,這就是上面為什么能登陸成功的原因所在。


那怎么解決這個問題呢?

答:利用PreparedStatement對象,不使用Statement對象。

PreparedStatement對象是Statement對象的子類,它是預編譯的sql,所以運行速度會比Statemnet更快。

PerpaerdStatement使用?作為占位符,使用setXxx(索引,值)給?賦值

所以我們替換一下Statement,寫一下代碼:

    public boolean login(String username, String password) {        if (username == null || password == null) {            return false;         }         Connection conn = null;         PreparedStatement pstmt = null;         ResultSet rs = null;         try {            //1、獲取數(shù)據(jù)庫連接             conn = JDBCUtils.getConnection();   //JDBCUtils類             //2、定義sql             String sql = "select * from user where username = ? and password = ?";             //3、獲取執(zhí)行sql的對象             pstmt = conn.prepareStatement(sql);             pstmt.setString(1,username);             pstmt.setString(2,password);             //4、執(zhí)行sql             rs = pstmt.executeQuery();             return rs.next();         } catch (SQLException e) {             e.printStackTrace();         } finally {             JDBCUtils.close(rs, pstmt, conn);         }        return false;     }

測試一下:
解決 SQL 注入問題
成功解決!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊11 分享