未找到预期的CSRF令牌。 您的会话是否已过期403

我正在尝试使用mkyong示例编写我的测试弹簧安全应用程序。

Spring Security: 4.0.0.RC1 Spring: 4.1.4.RELEASE 

我有以下安全配置:

    <!--  -->         

登录页面:

   


现在,当我尝试登录时,我得到403错误页面:

 Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'. 

描述:

 Access to the specified resource (Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.) has been forbidden. 

怎么了,怎么解决这个问题? 我在配置中评论了csrf ,但错误消息与csrf

我有同样的问题。 我使用thymeleaf和Spring启动,当我尝试在表单中发布数据时,我遇到了CSRF令牌问题。

这是我的工作解决方案:

  1. 添加此隐藏输入:

  2. WebSecurityConfig (扩展WebSecurityConfigurerAdapter )中,添加一个方法:

     private CsrfTokenRepository csrfTokenRepository() { HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); repository.setSessionAttributeName("_csrf"); return repository; } 

    并在方法configure()添加代码:

     @Override protected void configure(HttpSecurity http) throws Exception { http.csrf() .csrfTokenRepository(csrfTokenRepository()) 

我花了很多时间在这个问题上。 希望它可以帮助那些有同样问题的人。

如果你必须禁用它…

在Spring Security 4中,使用XML配置时默认启用CSRF 。 以前,它仅在默认情况下启用基于Java的配置。

根据Spring Security文档的第14.4.2节 :

从Spring Security 4.0开始,默认情况下使用XML配置启用CSRF保护。 如果要禁用CSRF保护,可以在下面看到相应的XML配置。

  ...  ...  

禁用CSRF保护听起来像个坏主意,不是吗?

如果使用Spring的Form Tag库,将自动包含CSRF令牌。 它还将HTML Escape表单元素值,使您的网站更安全,更安全。

 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>   

否则,将其添加到您的表单:

  

要@ St.Antario,请使用此代码在您的代码中启用CSRF

 @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .antMatcher("*/*").authorizeRequests() .antMatchers("/", "/login**").permitAll() .anyRequest().authenticated() .and().csrf().csrfTokenRepository(csrfTokenRepository()) .and().addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class); } private Filter csrfHeaderFilter() { return new OncePerRequestFilter() { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); if (csrf != null) { Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN"); String token = csrf.getToken(); if (cookie == null || token != null && !token.equals(cookie.getValue())) { // Token is being added to the XSRF-TOKEN cookie. cookie = new Cookie("XSRF-TOKEN", token); cookie.setPath("/"); response.addCookie(cookie); } } filterChain.doFilter(request, response); } }; } private CsrfTokenRepository csrfTokenRepository() { HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); repository.setHeaderName("X-XSRF-TOKEN"); //repository.setSessionAttributeName(("X-XSRF-TOKEN")); return repository; } } 

弹簧security.xml文件

                 

web.xml中

  contextConfigLocation /WEB-INF/applicationContext.xml, /WEB-INF/spring-security.xml  

添加添加jsp登录

 <%@page session="true"%> 

并输入隐藏: