Spring security下csrf token的认证
来源:互联网 发布:js表单验证表示时间 编辑:程序博客网 时间:2024/06/08 08:48
在上一篇博文《Springboot 1.5.1整合Spring security 4》中,我们碰到“Could not verify the provided CSRF token because your session was not found. ”错误, 经过分析,是因为我们成功登录后,spring security为了防止CSRF攻击,需要在每个页面中验证成功登录后创建的csrf token值,而我们在静态页面中又无法传递这个token, 今天我们将来说明下如何处理这个问题。
还是基于上篇博文的代码,由于我们在页面上需要动态获取csrf token, 这时我们可以在页面上引入JS 代码,使之成为动态网页。
1) 将http.csrf().disable();注释掉
@Override protected void configure(HttpSecurity http) throws Exception { //http.csrf().disable(); http.authorizeRequests() .antMatchers("/", "/springbootbase").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .failureUrl("/login?error") .permitAll() //5 .and() .logout().permitAll(); }
2) 将index.html 改成JSP 文件: index.jsp
将csrf token 作为表单的隐藏域一起提交即可解决
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"><head> <title>Hello World!</title></head><body> <h1 th:inline="text">Hello World</h1> <form th:action="@{/logout}" action="./logout" method="post"> <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> <input type="submit" value="Sign Out"/> </form></body></html>
3) 重启tomcat server, 运行结果与上一篇博文一致。
0 0
- Spring security下csrf token的认证
- 使用Spring Security出现spring security Could not verify the provided CSRF token 异常
- spring security CSRF 问题 Invalid CSRF Token 'null' was found on ......
- spring security CSRF 问题 Invalid CSRF Token 'null' was found on ......
- Spring Security csrf学习
- spring security下开启csrf,同时支持session.invalidate()调用
- spring security csrf 学习笔记
- spring boot security csrf ignore
- spring security的csrf防御机制在ajax中的应用
- 解决Spring Security 表单上传文件CSRF失效的问题
- Spring Security学习记录(四) -- JSON Web Token实践(下)
- spring security:基于MongoDB的认证
- spring-security + ldap的认证配置
- Spring Security-认证
- spring security LDAP 认证
- Spring security 认证-ch01
- spring security 数据认证
- spring security认证
- go语言学习之----读取配置文件的方式toml
- 20170214C语言提升08_指针_01变量及指针及数组
- 欢迎使用CSDN-markdown编辑器
- IPC-Messenger使用实例
- Java实现几种常见排序方法
- Spring security下csrf token的认证
- 淘宝技术这十年
- Spring cloud Eureka服务注册及发现(二)创建注册服务
- 新建服务器的相关开发配置-----jdk配置
- MYSQL 修改密码
- /*用数组模拟邻接表 超时×/
- java数据结构与算法_____选择排序
- Java之Java集合框架
- log4j日志使用说明