当前位置:网站首页>security CSRF Vulnerability Protection
security CSRF Vulnerability Protection
2022-08-02 00:07:00 【Three or two lines of program】
I. Overview of CSRF
xss exploits user trust in websites
csrf exploits the website's trust in the user's browser
Example
Second, CSRF defense
1. Defense strategy
Through the token synchronization mode, in addition to the default cookie parameter in each http request, the server generates a random string called csrf token. After opening, the httpsession saves a copy, and the front-end request arrivesWhen comparing the requested csrf token information with the server, if they are not equal, the http request will be rejected
Considering that the website may place external links, it is required to be idempotent when requesting, so there is no need to use CSRF tokens for methods such as HEAD OPTIONS TRACE. Forcible use may lead to token leakage
2. Traditional web development configuration
@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin().and()//Open csrf.csrf();}}
3. Front-end and back-end separation configuration
The default is that csrf is stored in the server httpsession. In front-end and back-end separation, it is necessary to put the generated csrf into the cookie and obtain the token in the cookie for submission when requesting.
Modify csrf into cookie
@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin().and()//Open csrf.csrf()//Save the token to the cookie and allow the cookie front end to get it.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());}}
Visit the login interface to view cookies
Send request with token
The first type: request other interfaces and carry it in the request header
X-XSRF-TOKEN:value in cookie
The second type: carry in the request parameters
_csrf:value in cookie
边栏推荐
猜你喜欢
云原生DevOps环境搭建
Various Joins of Sql
经典文献阅读之--DLO
Enterprise firewall management, what firewall management tools are there?
使用 Zadig 交付云原生微服务应用
【三子棋】C语言实现简易三子棋
Win11内存管理错误怎么办?
EasyExcel的简单读取操作
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
Appears in oozie on CDH's hue, error submitting Coordinator My Schedule
随机推荐
检查 Oracle 版本的 7 种方法
工作5年,测试用例都设计不好?来看看大厂的用例设计总结
字节跳动面试官:请你实现一个大文件上传和断点续传
递归:方法调用自身
如何用Redis实现分布式锁?
Sql之各种Join
学习英语的网站与资料
LocalDateTime转为Date类型
如何重装Win11?一键重装Win11方法
数据机构---第五章树与二叉树---二叉树的概念---应用题
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
@Transactional注解在类上还是接口上使用,哪种方式更好?
分享一份接口测试项目(非常值得练手)
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
Flink Yarn Per Job - 提交流程一
【Leetcode】470. Implement Rand10() Using Rand7()
Architecture basic concept and nature of architecture
使用Ganache、web3.js和remix在私有链上部署并调用合约
C语言七夕来袭!是时候展现专属于程序员的浪漫了!
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息