csrf是什么?
csrf(cross-site request forgery),中文名稱:跨站請求偽造,也被稱為:one click attack/session riding,縮寫為:csrf/xsrf。
csrf可以做什么?
你這可以這么理解csrf攻擊:攻擊者盜用了你的身份,以你的名義發(fā)送惡意請求。csrf能夠做的事情包括:以你名義發(fā)送郵件,發(fā)消息,盜取你的賬號,甚至于購買商品,虛擬貨幣轉(zhuǎn)賬......造成的問題包括:個人隱私泄露以及財產(chǎn)安全。
csrf漏洞現(xiàn)狀
csrf這種攻擊方式在2000年已經(jīng)被國外的安全人員提出,但在國內(nèi),直到06年才開始被關(guān)注,08年,國內(nèi)外的多個大型社區(qū)和交互網(wǎng)站分別爆出csrf漏洞,如:nytimes.com(紐約時報)、metafilter(一個大型的blog網(wǎng)站),youtube和百度hi......而現(xiàn)在,互聯(lián)網(wǎng)上的許多站點仍對此毫無防備,以至于安全業(yè)界稱csrf為“沉睡的巨人”。
在一個spring boot項目中,需要防止csrf攻擊,可以只把spring security中的相關(guān)filter引入來進行.
在pom中添加相關(guān)依賴
1
2
3
4
5
6
7
8
9
10
11
|
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-freemarker</artifactid> </dependency> <!-- security (used for csrf protection only) --> <dependency> <groupid>org.springframework.security</groupid> <artifactid>spring-security-web</artifactid> </dependency> </dependencies> |
在app啟動時,添加csrffilter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@springbootapplication public class application extends webmvcconfigureradapter { @bean public filterregistrationbean csrffilter() { filterregistrationbean registration = new filterregistrationbean(); registration.setfilter( new csrffilter( new httpsessioncsrftokenrepository())); registration.addurlpatterns( "/*" ); return registration; } public static void main(string[] args) { springapplication.run(application. class , args); } } |
form中添加csrf的hidden字段
1
|
<input name= "${(_csrf.parametername)!}" value= "${(_csrf.token)!}" type= "hidden" > |
ajax中添加csrf的頭
1
|
xhr.setrequestheader( "${_csrf.headername}" , "${_csrf.token}" ); |
github地址是 https://github.com/kabike/spring-boot-csrf
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jianshu.com/p/3b8b92657ce9