csrf漏洞現(xiàn)狀
csrf(cross-site request forgery)跨站請求偽造,也被稱為one click attack或者session riding,通常縮寫為csrf或xsrf,是一種對網(wǎng)站的惡意利用。盡管聽起來像跨站腳本(xss),但它與xss非常不同,xss利用站點內(nèi)的信任用戶,而csrf則通過偽裝成受信任用戶的請求來利用受信任的網(wǎng)站。與xss攻擊相比,csrf攻擊往往不大流行(因此對其進(jìn)行防范的資源也相當(dāng)稀少)和難以防范,所以被認(rèn)為比xss更具危險性。
csrf是一種依賴web瀏覽器的、被混淆過的代理人攻擊(deputy attack)。
pom依賴
1
2
3
4
5
6
7
8
9
10
|
<!-- 模板引擎 freemarker --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-freemarker</artifactid> </dependency> <!-- security (只使用csrf部分) --> <dependency> <groupid>org.springframework.security</groupid> <artifactid>spring-security-web</artifactid> </dependency> |
配置過濾器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@springbootapplication public class application { public static void main(string[] args) { springapplication.run(application. class , args); } /** * 配置csrf過濾器 * * @return {@link org.springframework.boot.web.servlet.filterregistrationbean} */ @bean public filterregistrationbean<csrffilter> csrffilter() { filterregistrationbean<csrffilter> registration = new filterregistrationbean<>(); registration.setfilter( new csrffilter( new httpsessioncsrftokenrepository())); registration.addurlpatterns( "/*" ); registration.setname( "csrffilter" ); return registration; } } |
在form請求中添加csrf的隱藏字段
1
|
<input name= "${(_csrf.parametername)!}" value= "${(_csrf.token)!}" type= "hidden" /> |
在ajax請求中添加header頭
1
|
xhr.setrequestheader( "${_csrf.headername}" , "${_csrf.token}" ); |
jquery的ajax全局配置
1
2
3
4
5
|
jquery.ajaxsetup({ "beforesend" : function (request) { request.setrequestheader( "${_csrf.headername}" , "${_csrf.token}" ); } }); |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000018402597