boke练习: springboot整合springSecurity出现的问题,post,delete,put无法使用
springboot 与 SpringSecurity整合后,为了防御csrf攻击,只有GET|OPTIONS|HEAD|TRACE|CONNECTION可以通过。
其他方法请求时,需要有token
解决方法:
1,支持post的方法:
1,如果使用freemarker模板
在form里添加<input type="hidden" name="${_csrf.parameterName}" value="_csrf.token">
2,使用ajax时
$.ajax({
url:"/manager",
type:"POST",
data:{
"${_csrf.parameterName}":"${_csrf.token}",
//其他的数据
}
})
2,支持delete,put的方法:
在支持post的基础上,
$.ajax({
url:"/manager",
type:"POST",
data:{
"${_csrf.parameterName}":"${_csrf.token}",
_method:"DELETE", /添加了这个,在后端就可以使用delete方法接收请求了,实现restful
//其他的数据
}
})
二、当开启CSRF后,原来以Get方式,调用/logout,退出登录状态的功能失效了,跳转后报404错误。
1、查看源码,发现框架方法里做了备注。大概意思就是开启CSRF后,logout方法需要以post方式提交。或者调用logoutRequestMatcher方法,显示设置/logout请求为GET方法
2、用logoutRequestMatcher设置logout为get请求来解决404报错问题。POST方式比较简单这里就不在赘述。当然,实际项目中,最好选择以post方式退出系统。
http
.formLogin().loginPage("/user/login.do")
.defaultSuccessUrl("/free/list.do")//启用FORM登录
.and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout","GET"))
至此CSRF防止方案示例就讲完了。在项目中,对于post请求都需要注意提交_csrf到服务端。希望对你有所帮助,欢迎留言交流。
boke练习: springboot整合springSecurity出现的问题,post,delete,put无法使用的更多相关文章
- boke练习: springboot整合springSecurity出现的问题,传递csrf
boke练习: springboot整合springSecurity出现的问题,传递csrf freemarker模板 在html页面中加入: <input name="_csrf&q ...
- SpringBoot整合SpringSecurity简单实现登入登出从零搭建
技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...
- SpringBoot整合SpringSecurity示例实现前后分离权限注解
SpringBoot 整合SpringSecurity示例实现前后分离权限注解+JWT登录认证 作者:Sans_ juejin.im/post/5da82f066fb9a04e2a73daec 一.说 ...
- 9、SpringBoot整合之SpringBoot整合SpringSecurity
SpringBoot整合SpringSecurity 一.创建项目,选择依赖 选择Spring Web.Thymeleaf即可 二.在pom文件中导入相关依赖 <!-- 导入SpringSecu ...
- SpringBoot整合SpringSecurity实现JWT认证
目录 前言 目录 1.创建SpringBoot工程 2.导入SpringSecurity与JWT的相关依赖 3.定义SpringSecurity需要的基础处理类 4. 构建JWT token工具类 5 ...
- springboot整合springsecurity遇到的问题
在整合springsecurity时遇到好几个问题,自动配置登录,下线,注销用户的操作,数据基于mybatis,模版引擎用的thymeleaf+bootstrap. 一.认证时密码的加密(passwo ...
- SpringBoot 整合 SpringSecurity 梳理
文档 Spring Security Reference SpringBoot+SpringSecurity+jwt整合及初体验 JSON Web Token 入门教程 - 阮一峰 JWT 官网 Sp ...
- SpringBoot整合SpringSecurity简单案例
在我们开发项目的过程中经常会用到一些权限管理框架,Java领域里边经常用的可能就是shiro了,与之对应的还有SpringSecurity,SpringSecurity可以说是非常强大,与Spring ...
- SpringBoot整合SpringSecurity,SESSION 并发管理,同账号只允许登录一次
重写了UsernamePasswordAuthenticationFilter,里面继承AbstractAuthenticationProcessingFilter,这个类里面的session认证策略 ...
随机推荐
- ldap集成jira
jira默认支持ldap,通过管理员登录jira 点击 User Management --> User Directories --> Add Directory. 进行ldap配置: ...
- 前端 --- 7 Bootstrop框架
一.基本结构 1.下载地址 官方地址:https://getbootstrap.com 中文地址:http://www.bootcss.com/ 2.HTML基本搭建结构 <!DOCTYPE ...
- topcoder srm 320 div1
problem1 link 两个数字后面都有阶乘符号,可以抵消. import java.util.*; import java.math.*; import static java.lang.Mat ...
- Pytest 简明教程
pytest-learn 通过文章 Python 单元测试框架之 Pytest 剖解入门(第一篇) 学习 Pytest. 有很多的第三方插件可以自定义扩展,并且支持 Allure,生成可视化的测试报告 ...
- leetcode 04 Median of Two Sorted Arrays
n1 为 num1的 len n2 为 num2的 len 故中间的数应该是 k = (n1 + n2 + 1) / 2 二分 num1中位置 m1 , 故 num2的位置为m2 必须保证 nums1 ...
- P5057 [CQOI2006]简单题(线段树)
果然简单题,5分钟紫题++ 代码 #include <cstdio> #include <algorithm> #include <cstring> using n ...
- 【Hadoop 分布式部署 二:分布式环境预备工作(主机名 IP地址等设置)】
1.首先使用工具连接上 这三台虚拟主机 2.配置主机名 切换到 root 用户 第一种方式 可以使用命令 hostname [要更改的主机名] 但是这种更改主机名的方式 ...
- org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup' from result set. Cause: java.sql.SQLException: Error
异常展示: org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup ...
- CAS Client集群环境的Session问题及解决方案介绍,下篇介绍作者本人项目中的解决方案代码
CAS Client集群环境的Session问题及解决方案 程序猿讲故事 2016-05-20 原文 [原创申明:文章为原创,欢迎非盈利性转载,但转载必须注明来源] 之前写过一篇文章,介绍单点登 ...
- linux 基础命令(12月25日笔记)
1. cp指令指令:cp (copy,复制)作用:复制文件/文件夹到指定的位置语法:#cp [-r] 被复制的文档路径 文档被复制到的路径选项: -r:recurs ...