2016-09-28 62 views
2

我在我的项目中使用了spring security和Java配置。我们可以指定CSRF令牌到期超时吗?

缺省情况下,弹簧安全的Java配置启用了csrf。

是否可以设置超时之后csrf令牌过期?这是为基于令牌的应用程序指定超时的要求。

经过一些博客和文章后,我注意到csrf令牌的行为是不可预知的,使它更安全。

以下是用于配置弹簧安全性的示例代码。

@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() 
    } 

如果有某种方式可以设置超时时间,从而为我节省大量工作。

回答

0

Spring Security Reference

一个问题是,预期的CSRF令牌,以便尽快在HttpSession到期存储在HttpSession中,你的配置AccessDeniedHandler会收到InvalidCsrfTokenException。

这意味着,你可以改变会话超时在web.xml到期的CSRF令牌,例如参见WebLogic

<session-timeout> |可选|之后,在此Web应用程序会话过期分钟数

另一种方式是写自己的CsrfTokenRepository

一个API,允许改变其预期CsrfToken被关联到HttpServletRequest方法。例如,它可以存储在HttpSession中。