当前位置:网站首页>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中得值
边栏推荐
- 数据机构---第五章树与二叉树---二叉树的概念---应用题
- [LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
- ELK log collection
- 20220725 Information update
- yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
- 很多人喜欢用多御安全浏览器,竟是因为这些原因
- 仿牛客网项目第三章:开发社区核心功能(详细步骤和思路)
- @Resource和@Autowired的区别
- 检查 Oracle 版本的 7 种方法
- Programmer is still short of objects? A new one is enough
猜你喜欢
检查 Oracle 版本的 7 种方法
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
Excel表格数据导入MySQL数据库
【MySQL篇】初识数据库
Classical Literature Reading--DLO
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
随机推荐
@Resource和@Autowired的区别
@Transactional 注解使用详解
如何进行数据库备份
Excel导入和导出
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
TexturePacker使用文档
Additional Features for Scripting
Spark Sql之join on and和where
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
DOM 基础操作
在MySQL中使用MD5加密【入门体验】
cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
[Camp Experience Post] 2022 Cybersecurity Summer Camp
架构基本概念和架构本质
Flink Yarn Per Job - 提交流程一
【ACWing】230. 排列计数
Get piggy homestay (short-term rental) data
6133. 分组的最大数量
【Leetcode】475. Heaters
Work for 5 years, test case design is bad?To look at the big case design summary