当前位置:网站首页>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中得值
边栏推荐
- ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
- 获取小猪民宿(短租)数据
- 6132. All the elements in the array is equal to zero - quick sort method
- 切面打印调取的方法
- 很多人喜欢用多御安全浏览器,竟是因为这些原因
- 架构基本概念和架构本质
- An interview question about iota in golang
- Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
- 检查 Oracle 版本的 7 种方法
- [C language advanced] file operation (2)
猜你喜欢

oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed

2022 6th Strong Net Cup Part WP

Work for 5 years, test case design is bad?To look at the big case design summary

中职网络安全竞赛B7比赛部署流程

C language - branch statement and loop statement

DOM 基础操作

Thinkphp 5.0.24变量覆盖漏洞导致RCE分析

Secondary Vocational Network Security Competition B7 Competition Deployment Process

cmd command

深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
随机推荐
辛普森悖论
Quartus 使用 tcl 文件快速配置管脚
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
【图像融合】基于加权和金字塔实现图像融合附matlab代码
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
Convert LocalDateTime to Date type
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
async和await用法介绍
@Scheduled注解详解
Various Joins of Sql
很多人喜欢用多御安全浏览器,竟是因为这些原因
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
颜色透明参数
cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
获取小猪民宿(短租)数据
6134. Find the closest node to the given two nodes - force double hundred code
Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
工作5年,测试用例都设计不好?来看看大厂的用例设计总结
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
Department project source code sharing