我启用了带有 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:
- The client asks a token with an OPTIONS request.
- The server creates a temporary session, stores the token and sends back a JSESSIONID and the token to the client.
- The client submits the login credentials using that JSESSIONID and CSRF token.
- 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 种策略。
默认行为 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-timeout
在 web.xml
使用 session-config
. Tomcat
的默认值是 30 分钟。
关于spring - Spring 临时 CSRF token 过期时间是多久?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35459333/