当前位置:网站首页>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
边栏推荐
- Work for 5 years, test case design is bad?To look at the big case design summary
- 信息系统项目管理师必背核心考点(五十七)知识管理工具
- QML包管理
- 一个有些意思的项目--文件夹对比工具(一)
- 根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
- YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
- 斜堆、、、
- 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
- 多御安全浏览器android版更新至1.7,改进加密协议
猜你喜欢
随机推荐
切面打印调取的方法
GIF制作-灰常简单的一键动图工具
【Leetcode】479. Largest Palindrome Product
中职网络安全竞赛B7比赛部署流程
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
【MySQL篇】初识数据库
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
Appears in oozie on CDH's hue, error submitting Coordinator My Schedule
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
12306抢票,极限并发带来的思考?
ES中SQL查询详解
Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
Sql之各种Join
经典文献阅读之--DLO
Win11如何获得最佳电源效率?
security 会话并发管理
20220725资料更新
background-image使用
Work for 5 years, test case design is bad?To look at the big case design summary
路径压缩、、









