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到服务端。希望对你有所帮助,欢迎留言交流。
- springboot整合springSecurity出现的问题,post,delete,put无法使用
- boke练习: springboot整合springSecurity出现的问题,传递csrf
- 关于spring boot整合mybatis使用oracle数据库出现could not load:oracle.jdbc.driver.OracleDriver问题的终极解决方案
- 使用Spring整合Hibernate出现无法自动建立表的问题
- 使用springboot+maven出现的问题
- Maven使用package打包Spring Boot时出现:Unable to find a single main class from the following candidates的问题解决
- spring boot整合shiro引用配置文件配置是出现的问题
- IntelliJ IDEA中Spring Boot项目使用spring-boot-devtools无法实现热部署/热更新的问题解决
- 当使用ajax 由前台像后台post 数组类型数据的一些问题(基于springboot)
- Maven项目中,关于Spring Boot 整合MyBatis时,Service层无法找到mapper接口的问题解决
- struts2.3整合spring3.2出现无法注入的问题
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- Spring Boot整合Activiti,查看流程图出现中文乱码问题
- Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决
- SpringBoot使用多实例QUARTZ出现重复执行问题
- 关于CXF整合Spring时出现jaxws.xsd无法找到的问题
- Angular2 和springboot 整合后 url 解析出现的问题解决方案
- Springmvc+ResteasyClient 整合无法Post对象的问题
- 关于IDEA工具在springboot整合mybatis中出现的Invalid bound statement (not found)问题
- 在idea里使用SpringBoot整合MyBatis时遇到的Mapper扫描不到的问题