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认证策略 ...
随机推荐
- UVa Live 3942 Remember the Word - Hash - 动态规划
题目传送门 高速路出口I 高速路出口II 题目大意 给定若干种短串,和文本串$S$,问有多少种方式可以将短串拼成长串. 显然,你需要一个动态规划. 用$f[i]$表示拼出串$S$前$i$个字符的方案数 ...
- 使用JBarcode生成一维码
需要的jar包,只有jbarcode.jar 链接: https://pan.baidu.com/s/1o9oDPB8 密码: x367 public class Main { //设置条形码高度 p ...
- topcoder srm 465 div1
problem1 link 以两个点$p,q$为中心的两个正方形的边长和最大为$2dist(p,q)$,即$p,q$距离的两倍. 也就是两个$p,q$的连线垂直穿过两个正方形的一对边且平分两个正方形. ...
- ng-model绑定的是ng-option中的什么?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- HDU1285 确定比赛问题【拓扑排序+优先队列】
题目 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩, ...
- Excel lastindex of a substring
I think I get what you mean. Let's say for example you want the right-most \ in the following string ...
- 题解——Codeforces Round #507 (based on Olympiad of Metropolises) T1 (模拟)
暴力模拟即可 就是情况略多 #include <cstdio> #include <algorithm> #include <cstring> using name ...
- 取消开机logo,改成代码刷屏
将开机logo改成开始时代码刷屏,这样就能很方便看到开始时的一些问题 首先 sudo chmod 666 /etc/default/grub 然后将 GRUB_CMDLINE_LINUX_DEFAUL ...
- Console的9种用法
Console的9种用法,1.显示信息的命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!DOCTYPE html> <html> <he ...
- Git、GitHub、GitLab三者之间的联系以及区别
在讲区别以及联系之前先简要的介绍一下,这三者都是什么(本篇文章适合刚入门的新手,大佬请出门左转) 1.什么是 Git? Git 是一个版本控制系统. 版本控制是一种用于记录一个或多个文件内容变化,方便 ...