欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解如何在spring boot中使用spring security防止CSRF攻擊

 更新時間:2018年05月07日 09:38:15   作者:大神帶我來搬磚  
這篇文章主要介紹了詳解如何在spring boot中使用spring security防止CSRF攻擊,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

CSRF是什么?

CSRF(Cross-site request forgery),中文名稱:跨站請求偽造,也被稱為:one click attack/session riding,縮寫為:CSRF/XSRF。

 CSRF可以做什么?

你這可以這么理解CSRF攻擊:攻擊者盜用了你的身份,以你的名義發(fā)送惡意請求。CSRF能夠做的事情包括:以你名義發(fā)送郵件,發(fā)消息,盜取你的賬號,甚至于購買商品,虛擬貨幣轉賬......造成的問題包括:個人隱私泄露以及財產(chǎn)安全。

CSRF漏洞現(xiàn)狀

CSRF這種攻擊方式在2000年已經(jīng)被國外的安全人員提出,但在國內(nèi),直到06年才開始被關注,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中的相關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

@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字段

<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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論