CSRF是什么?
CSRF(Cross-site request forgery),中文名稱:跨站請求偽造,也被稱為:one click attack/session riding,縮寫為:CSRF/XSRF。
CSRF可以做什么?
你這可以這么理解CSRF攻擊:攻擊者盜用了你的身份,以你的名義發送惡意請求。CSRF能夠做的事情包括:以你名義發送郵件,發消息,盜取你的賬號,甚至于購買商品,虛擬貨幣轉賬......造成的問題包括:個人隱私泄露以及財產安全。
CSRF漏洞現狀
CSRF這種攻擊方式在2000年已經被國外的安全人員提出,但在國內,直到06年才開始被關注,08年,國內外的多個大型社區和交互網站分別爆出CSRF漏洞,如:NYTimes.com(紐約時報)、Metafilter(一個大型的BLOG網站),YouTube和百度HI......而現在,互聯網上的許多站點仍對此毫無防備,以至于安全業界稱CSRF為“沉睡的巨人”。
在一個spring boot項目中,需要防止CSRF攻擊,可以只把spring security中的相關filter引入來進行.
在pom中添加相關依賴
<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
@SpringBootApplicationpublic 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字段
<input name="${(_csrf.parameterName)!}" value="${(_csrf.token)!}" type="hidden">
ajax中添加CSRF的頭
xhr.setRequestHeader("${_csrf.headerName}", "${_csrf.token}");
github地址是https://github.com/kabike/spring-boot-csrf
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選