【问题标题】:How to enhance csrf protection for existing spring boot and anjularjs application?如何增强现有 spring boot 和 angularjs 应用程序的 csrf 保护?
【发布时间】:2020-03-22 23:55:06
【问题描述】:

我需要为我现有的 Spring Boot 应用程序添加 csrf 保护,其中 angularJS 是客户端框架。

【问题讨论】:

    标签: angularjs spring-boot csrf


    【解决方案1】:

    spring-security 需要设置 cookie 'XSRF-TOKEN ' 但不在 HttpOnly 中。然后 Angular 的 javascript 会读取 cookie 并将其添加到传出的 http 请求的 headers 中。

    在你的 spring-boot 代码库中应该有一个类似于:

    @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { // class content }

    在这个类中应该有一个以'http'开头的链。

    在这个链中添加 .csrf(csrf -> csrf .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) )

    来自文档:“默认情况下,CookieCsrfTokenRepository 将写入名为 XSRF-TOKEN 的 cookie,并从名为 X-XSRF-TOKEN 的标头或 HTTP 参数 _csrf 中读取它。这些默认值来自 AngularJS”。

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2018-07-31
      • 2012-02-16
      • 2021-04-17
      • 2020-01-08
      • 1970-01-01
      • 2017-08-09
      • 2015-11-30
      • 2018-11-04
      相关资源
      最近更新 更多