异常:Could not verify the provided CSRF token because your session was not found.

本项目是SpringBoot项目,模板引擎是thymeleaf
pom文件中 主要依赖为:
-
<parent> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-parent</artifactId> -
<version>1.5.9.RELEASE</version> -
<relativePath/> <!-- lookup parent from repository --> -
</parent> -
<properties> -
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> -
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> -
<java.version>1.8</java.version> -
</properties> -
<dependencies> -
<dependency> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-data-jpa</artifactId> -
</dependency> -
<dependency> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-data-redis</artifactId> -
</dependency> -
<dependency> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-thymeleaf</artifactId> -
</dependency> -
<dependency> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-web</artifactId> -
</dependency> -
<!--security--> -
<dependency> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-security</artifactId> -
</dependency> -
<!-- 模板引擎--> -
<dependency> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-thymeleaf</artifactId> -
</dependency> -
<dependency> -
<groupId>mysql</groupId> -
<artifactId>mysql-connector-java</artifactId> -
<scope>runtime</scope> -
</dependency> -
<dependency> -
<groupId>org.projectlombok</groupId> -
<artifactId>lombok</artifactId> -
<optional>true</optional> -
</dependency> -
<dependency> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-test</artifactId> -
<scope>test</scope> -
</dependency> -
</dependencies>
原因:
Spring Security4默认是开启CSRF的,所以需要请求中包含CSRF的token信息,在其官方文档(参考资料1)中,提供了在form中嵌入一个hidden标签来获取token信息,其原理是,hidden标签使用了Spring Security4提供的标签,即${_csrf.parameterName}、${_csrf.token}, 后台页面渲染过程中,将此标签解所对应的值解析出来,这样,我们的form表单,就嵌入了Spring Security的所需的token信息,在后续的提交登录请求时,就不会出现没有CSRF token的异常。
另外,还有一个解决办法是,通过关闭CSRF来解决,这个几乎在任何场景中都能解决这个问题(上面这个解决方案,可能在某些渲染模板不能解析出来token值,不过可以通过后台程序来获取token值,然后自己定义变量来渲染到form中,这个也是可以的)。具体的做法是通过修改配置文件来关闭,我这里使用的是SpringBoot开发的项目,配置文件直接写在配置类中,通过.csrf().disable()来关闭,参考资料见二。不过这种方案,会迎来CSRF攻击,不建议在生产环境中使用,如果系统对外界做了隔离,这样做也是可以的。
解决办法:
1、在from表单中加入一个hidden标签,来引用spring security 的csrf token。

<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
如果是jsp做模板引擎,则可以使用:
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
2、通过关闭csrf来解决:
-
@Configuration -
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {//1 -
@Bean -
UserDetailsService customUserService() { //2 -
return new UserService(); -
} -
// 配置登陆校验,登陆成功后,会保存session -
@Override -
protected void configure(AuthenticationManagerBuilder auth) throws Exception { -
auth.userDetailsService(customUserService()); //3 -
} -
// 配置默认登陆页面,登陆失败页面 -
@Override -
protected void configure(HttpSecurity http) throws Exception { -
http.authorizeRequests() -
.anyRequest().authenticated() -
.and() -
.csrf().disable() //关闭CSRF -
.formLogin() -
.loginPage("/login") -
.failureUrl("/login?error") -
.permitAll() -
.and() -
.logout().permitAll(); -
} -
}
本文介绍了SpringBoot项目中遇到的CSRF验证失败异常,并提供了解决方案,包括在表单中加入隐藏字段传递token和完全禁用CSRF保护两种方法。
523

被折叠的 条评论
为什么被折叠?



