spring - Spring 临时 CSRF token 过期时间是多久?

标签 spring session csrf

我启用了带有 Spring Security 的 CSRF,它按预期工作。

我阅读了有关 CSRF 的 Spring 官方文档
http://docs.spring.io/spring-security/site/docs/3.2.7.RELEASE/reference/htmlsingle/#csrf

我还阅读了这篇关于 CSRF with Spring and AngularJS 的教程
http://www.codesandnotes.be/2015/07/24/angularjs-web-apps-for-spring-based-rest-services-security-the-server-side-part-2-csrf/

What Spring Security does is that it sets up a temporary session for that. So basically it goes like this:

  1. The client asks a token with an OPTIONS request.
  2. The server creates a temporary session, stores the token and sends back a JSESSIONID and the token to the client.
  3. The client submits the login credentials using that JSESSIONID and CSRF token.
  4. The server matches the CSRF stored for the received JSESSIONID and, if all is green-lighted, creates a new definitive JSESSIONID and a new session-based CSRF token for the client to validate its requests after the login.


据我了解,当您未登录时,您可以通过在任何 API 端点上发送 OPTIONS 请求来获取您的第一个 CSRF token ,例如/api/login

然后 Spring 将创建一个绑定(bind)到临时 session 的 CSRF token (临时 CSRF 和 JSESSIONID cookie)

因此,如果我询问 CSRF token 而不是等待几分钟最后尝试登录,CSRF token 可能已经过期,我将不得不询问另一个 token 。

我找不到如何配置临时 Spring session 到期时间,也找不到它的确切默认持续时间。

有没有人有这方面的信息?

最佳答案

creates a new definitive JSESSIONID and a new session-based CSRF token



这是 session fixation战略。

CSRFToken 生成至少有 2 种策略。
  • per session
  • per request

  • 默认行为 should be per session .这意味着只要 session 是 alive一个也是唯一的 CSRFToken 将绑定(bind)到它(但这可以更改)。
    成功认证后,由于 session 固定,将使用新的 CSRFToken 创建一个新 session 。

    Thus, if I ask the CSRF token than wait a few minutes and finally try to login, the CSRF token may have expîred and I will have to ask another one



    这是错误的。只要 session 处于事件状态,它就会一直存在。

    I couldn't find how to configure the temporary Spring session expiration time and I couldn't find what was its exact default duration


    temporary session被称为 temporary ,因为它在身份验证之前一直有效,并且会被新的替换。但与 common 相同的超时策略适用于它们。 session 。您可以配置session-timeoutweb.xml使用 session-config . Tomcat的默认值是 30 分钟。

    关于spring - Spring 临时 CSRF token 过期时间是多久?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35459333/

    相关文章:

    java - 如何验证 DTO 中的枚举?

    java - 如何从 Jackson 中的自定义解串器调用默认解串器

    php - 如何使用 session ID 获取 session 变量

    node.js - Node 快速 session - secret 是否需要以某种方式独一无二?

    testing - 如何在启用 CSRF 安全性的情况下测试 Sails.js v1.0 登录 Controller (使用 mocha、supertest)?

    spring - Flyway/Spring 和 H2 嵌入式数据库的 Schema 相关问题

    java - Spring MVC 3.2.1 中未出现 Json 响应

    java - Multi-Tenancy Hibernate 实现中的 getCurrentSession

    javascript - CSRF 403 禁止 - 无效的 CSRF token

    node.js - 在 Express 中使用 csurf 和 session