我们可以指定 CSRF 令牌过期超时吗?
Can we specify the CSRF token expiry timeout?
我在我的项目中使用 spring 安全和 Java 配置。
spring 安全的 Java 配置默认启用 csrf。
是否可以设置 csrf 令牌过期后的超时时间?这是为基于令牌的应用程序指定超时的要求。
在浏览了一些博客和文章后,我注意到 csrf 令牌的行为是不可预测的,因此更加安全。
这是配置 spring 安全性的示例代码。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().maximumSessions(1).expiredUrl("/login-expired.html").and().and()
.authorizeRequests().antMatchers("/superadmin/**").access("hasRole('ROLE_SUPER_ADMIN')").and()
.formLogin().loginPage("/signin.html").permitAll().failureUrl("/login-failed.html").permitAll()
.and().exceptionHandling().accessDeniedPage("/403").and().logout().permitAll().and()
.exceptionHandling().and().logout().logoutSuccessUrl("/logout.html").permitAll().and()
}
如果有什么方法可以设置超时,那将节省我很多工作。
One issue is that the expected CSRF token is stored in the HttpSession, so as soon as the HttpSession expires your configured AccessDeniedHandler
will receive a InvalidCsrfTokenException.
这意味着,您可以更改 web.xml
中的会话超时以使 CSRF 令牌过期,例如 WebLogic:
<session-timeout>
| optional | The number of minutes after which sessions in this Web application expire
另一种方法是自己编写CsrfTokenRepository
:
An API to allow changing the method in which the expected CsrfToken
is associated to the HttpServletRequest
. For example, it may be stored in HttpSession
.
我在我的项目中使用 spring 安全和 Java 配置。
spring 安全的 Java 配置默认启用 csrf。
是否可以设置 csrf 令牌过期后的超时时间?这是为基于令牌的应用程序指定超时的要求。
在浏览了一些博客和文章后,我注意到 csrf 令牌的行为是不可预测的,因此更加安全。
这是配置 spring 安全性的示例代码。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().maximumSessions(1).expiredUrl("/login-expired.html").and().and()
.authorizeRequests().antMatchers("/superadmin/**").access("hasRole('ROLE_SUPER_ADMIN')").and()
.formLogin().loginPage("/signin.html").permitAll().failureUrl("/login-failed.html").permitAll()
.and().exceptionHandling().accessDeniedPage("/403").and().logout().permitAll().and()
.exceptionHandling().and().logout().logoutSuccessUrl("/logout.html").permitAll().and()
}
如果有什么方法可以设置超时,那将节省我很多工作。
One issue is that the expected CSRF token is stored in the HttpSession, so as soon as the HttpSession expires your configured
AccessDeniedHandler
will receive a InvalidCsrfTokenException.
这意味着,您可以更改 web.xml
中的会话超时以使 CSRF 令牌过期,例如 WebLogic:
<session-timeout>
| optional | The number of minutes after which sessions in this Web application expire
另一种方法是自己编写CsrfTokenRepository
:
An API to allow changing the method in which the expected
CsrfToken
is associated to theHttpServletRequest
. For example, it may be stored inHttpSession
.