当前位置:网站首页>security CSRF漏洞保护
security CSRF漏洞保护
2022-08-01 23:52:00 【程序三两行】
一、CSRF概述
跨站请求伪造或者一键式攻击通常缩写为csrf或者xsrf,通过挟持当前浏览器已经登录用户发送恶意请求的攻击烦方法
xss利用用户对网站的信任
csrf利用网站对用户浏览器的信任
举例

二、CSRF防御
1、防御策略
通过令牌同步模式 在每一次http请求中除了默认的cookie参数之外,服务端生成一个随机字符串称为csrf令牌,开启后httpsession保存一份, 前端请求到达时会将请求的csrf令牌信息和服务端对比 ,如果不相等则拒绝http请求
考虑到网站可能放置外部链接,所以要求请求时幂等的 这样对于HEAD OPTIONS TRACE 等方法就没有必要使用CSRF令牌了 强行使用可能会导致令牌泄露
2、传统web开发配置
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
//开启csrf
.csrf();
}
}
3、前后端分离配置
默认是csrf是保存在服务端httpsession中,前后端分离中需要将生成csrf放入到cookie中 并在请求时获取cookie中令牌进行提交即可
修改csrf放入到cookie
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
//开启csrf
.csrf()
//将令牌保存到cookie 并且允许cookie前端获取
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
}访问登录界面查看cookie

发送请求携带令牌
第一种:请求其他接口 在请求头中携带
X-XSRF-TOKEN:cookie中得值
第二种:请求参数中携带
_csrf:cookie中得值
边栏推荐
- Share an interface test project (very worth practicing)
- Flink Yarn Per Job - CliFrontend
- [Camp Experience Post] 2022 Cybersecurity Summer Camp
- Department project source code sharing
- Flink学习第五天——Flink可视化控制台依赖配置和界面介绍
- 【MySQL系列】 MySQL表的增删改查(进阶)
- 2022第六届强网杯部分wp
- numpy.where
- C language - branch statement and loop statement
- 高效工作文档产出归类
猜你喜欢
随机推荐
@Transactional注解在类上还是接口上使用,哪种方式更好?
获取小猪民宿(短租)数据
C language - branch statement and loop statement
【ACWing】406. 放置机器人
Secondary Vocational Network Security Competition B7 Competition Deployment Process
6132. 使数组中所有元素都等于零-快速排序法
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
sys_kill system call
【MySQL系列】MySQL索引事务
recursion: method calls itself
Programmer is still short of objects? A new one is enough
怎样做才能让这条SQL变成一条危险的SQL?
numpy.unique
数据机构---第五章树与二叉树---二叉树的概念---应用题
cdh6打开oozieWeb页面,Oozie web console is disabled.
DOM 基础操作
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
在linux下MySQL的常用操作命令


![[Camp Experience Post] 2022 Cybersecurity Summer Camp](/img/1e/716bafc679dc67d3d54bcc21a3b670.png)






