【问题标题】:CSRF token is configured but still POST requests are not working in spring boot appCSRF 令牌已配置,但 POST 请求仍然无法在 Spring Boot 应用程序中工作
【发布时间】:2021-08-13 19:43:51
【问题描述】:

我有一个 Starnd Spring Boot 应用程序,并在我的 sprint 安全配置中配置了 csrf,如下所示:

http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and().authorizeRequests()
                .mvcMatchers(HttpMethod.POST,"/slam").authenticated()
                .antMatchers("/myPage").authenticated()
                .antMatchers("/login").permitAll()
                .antMatchers("/contact").denyAll()
                .and().formLogin().and().httpBasic();

我正在使用 CookieCsrfTokenRepository 并使用 Postman 来命中 slam API 我在点击这个 API 之前已经对自己进行了身份验证,并从服务器端获得了 JSESSIONI、XSRF-TOKEN 并将其在标头中传递给 slam API

在此之后,我只是发送请求,但我仍然收到 403 禁止。 另一方面,如果我使用以下配置禁用 CSRF 令牌。发布请求正在成功运行。

http.csrf().disable()

通过我的 POSTMAN 的 post 请求使 CSRF 令牌正常工作我缺少什么?

【问题讨论】:

    标签: spring spring-boot spring-security csrf


    【解决方案1】:

    spring 的文档说明如下:

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

    在您提供的邮递员图像中,我只能看到 xsrf-token 作为 cookie,但没有标题。您需要从该 cookie 中提取值并将其发送回适当的标头或适当的查询参数。

    【讨论】:

      猜你喜欢
      • 2017-06-12
      • 2016-05-02
      • 2016-08-21
      • 2018-06-14
      • 2017-01-10
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多