当前位置:网站首页>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中得值
边栏推荐
- Making a Simple 3D Renderer
- 辛普森悖论
- 机器学习文本分类
- How to better understand and do a good job?
- CDH6 Hue to open a "ASCII" codec can 't encode characters
- Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程
- Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
- Flink学习第三天——一文带你了解什么是Flink流?
- Chrome书签插件,让你实现高效整理
- thinkphp漏洞总结
猜你喜欢

【Leetcode】1206. Design Skiplist

Flink学习第三天——一文带你了解什么是Flink流?

2022 6th Strong Net Cup Part WP

使用Ganache、web3.js和remix在私有链上部署并调用合约

数据机构---第五章树与二叉树---二叉树的概念---应用题

经典文献阅读之--DLO

Get piggy homestay (short-term rental) data

Appears in oozie on CDH's hue, error submitting Coordinator My Schedule

async和await用法介绍

【MySQL系列】 MySQL表的增删改查(进阶)
随机推荐
numpy.where
@Scheduled注解详解
Classical Literature Reading--DLO
递归:方法调用自身
问题解决方式了
cdh6打开oozieWeb页面,Oozie web console is disabled.
Department project source code sharing
几道关于golang并发的面试题
Work for 5 years, test case design is bad?To look at the big case design summary
正则表达式
Loading configuration of Nacos configuration center
problem solved
LocalDateTime转为Date类型
Flink Yarn Per Job - 提交流程一
Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
Quartus uses tcl files to quickly configure pins
【Leetcode】479. Largest Palindrome Product
Chapter 19 Tips and Traps: Common Goofs for Novices
切面打印调取的方法
仿牛客网项目第三章:开发社区核心功能(详细步骤和思路)