ibatis like 用法,各資料庫的安全拼接方法__資料庫

來源:互聯網
上載者:User

 原文地址:點擊開啟連結

iBatis解決sql注入

(1) ibatis xml配置:下面的寫法只是簡單的轉義 namelike '%$name$%'

(2) 這時會導致sql注入問題,比如參數name傳進一個單引號“'”,產生的sql語句會是:name like '%'%'

(3) 解決方案是利用字串串連的方式來構成sql語句 name like '%'||'#name#'||'%'

(4) 這樣參數都會經過先行編譯,就不會發生sql注入問題了。

(5) #與$區別:

#xxx# 代表xxx是屬性值,map裡面的key或者是你的pojo對象裡面的屬性,ibatis會自動在它的外面加上引號,表現在sql語句是這樣的 where xxx = 'xxx' ;

$xxx$ 則是把xxx作為字串拼接到你的sql語句中, 比如 order by topicId , 語句這樣寫 ... order by #xxx#,ibatis 就會把他翻譯成 order by 'topicId' (這樣就會報錯) 語句這樣寫 ... order by $xxx$ibatis 就會把他翻譯成 order by topicId   SELECT *    FROM   user     WHERE  username  like   '%$ username$ %'   的安全寫法

Sql代碼 SELECT *           FROM   user           WHERE  username  like   '%'   || #username# ||  '%'   

SELECT * FROM user WHERE username like '%' || #username# || '%' 

       其實上面的語句是針對Oracle 的,對於不同資料字串串連符不一樣。現列舉mysql和SQLServer如下:

     

Mysql Sql代碼 SELECT *           FROM   user           WHERE  username  like  CONCAT( '%' , #username#,  '%' )  

SQLServer: Sql代碼 SELECT *           FROM   user           WHERE  username  like   '%'  + #username# +   '%'   

-----------------------------------------------------------------------------------------------------------------------------

      關於資料庫字串串連符簡單列舉我使用過的一些資料庫如下:

Oracle SQLServer Mysql DB2
|| 或 CONCAT() + CONCAT() || 或 CONCAT()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.