当前位置:网站首页>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中得值
边栏推荐
- problem solved
- 【图像融合】基于加权和金字塔实现图像融合附matlab代码
- asyncawait和promise的区别
- 工作5年,测试用例都设计不好?来看看大厂的用例设计总结
- 【Leetcode】2360. Longest Cycle in a Graph
- The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
- Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
- The Spark of Sql join on the and and where
- 12306抢票,极限并发带来的思考?
- Share an interface test project (very worth practicing)
猜你喜欢

Excel导入和导出

yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;

Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections

nodejs--process

@WebServlet注解(Servlet注解)

ES中SQL查询详解

一款简洁的文件传输工具

多御安全浏览器android版更新至1.7,改进加密协议

Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions

WEB安全基础 - - - XRAY使用
随机推荐
2022第六届强网杯部分wp
正则表达式
Convert LocalDateTime to Date type
中职网络安全竞赛B7比赛部署流程
Chapter 11 Working with Dates and Times
What can be done to make this SQL into a dangerous SQL?
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
辛普森悖论
伸展树的特性及实现
recursion: method calls itself
Work for 5 years, test case design is bad?To look at the big case design summary
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
Department project source code sharing
The Spark of Sql join on the and and where
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
Quartus uses tcl files to quickly configure pins
获取小猪民宿(短租)数据
Flink Yarn Per Job - 提交流程一
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环