Fetch & Headers & CSRF
Fetch & Headers & CSRF
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Headers
X-Custom-Header
const myHeaders = new Headers();
myHeaders.append('Content-Type', 'text/plain');
myHeaders.append('X-Custom-Header', 'ProcessThisImmediately');
const content = 'Hello World';
myHeaders.append('Content-Length', content.length.toString());
// The same can be achieved by passing an array of arrays or an object literal to the constructor:
const myHeaders = new Headers({
'Content-Type': 'text/plain',
'Content-Length': content.length.toString(),
'X-Custom-Header': 'ProcessThisImmediately'
});
headers: {
"Content-Type": "application/json",
"x-csrf-token": csrftoken,
// .setRequestHeader('x-csrf-token', csrftoken);
},
CSRF 攻击:伪造用户请求向网站发起恶意请求。
message: "invalid csrf token"
https://eggjs.org/zh-cn/core/security.html#安全威胁csrf的防范
https://eggjs.org/zh-cn/core/security.html#安全威胁-csrf-的防范
// CSRF
// config/config.default.js
// module.exports = {
// security: {
// csrf: {
// ignoreJSON: true, // 默认为 false,当设置为 true 时,将会放过所有 content-type 为 `application/json` 的请求
// },
// },
// };
config.security = {
csrf: {
enable: false,
},
};
demo
text
const url = `http://localhost:7001/product/create`;
const json = { id: '123', name: 'admin' };
const csrftoken = document.cookie.split(';').map(item => item.trim()).map(item => ({[item.split(`=`)[0]]: item.split(`=`)[1]})).filter(obj => obj.csrfToken)[0].csrfToken;;
fetch(url, {
headers: {
"Content-Type": "application/json",
"x-csrf-token": csrftoken,
// .setRequestHeader('x-csrf-token', csrftoken);
},
// credentials: "same-origin",// cookie
method: "POST",
// mode: "cors",
body: JSON.stringify(json),
})
.then(res => res.text())
// .then(res => res.json())
.then(json => {
console.log(`text =`, json);
// console.log(`json =`, JSON.stringify(json, null, 4));
return json;
})
.catch(err => console.error(`error =`, err));
POST

JSON
const url = `http://localhost:7001/product/create`;
const json = { id: '123', name: 'admin' };
const csrftoken = document.cookie.split(';').map(item => item.trim()).map(item => ({[item.split(`=`)[0]]: item.split(`=`)[1]})).filter(obj => obj.csrfToken)[0].csrfToken;;
fetch(url, {
headers: {
"Content-Type": "application/json",
"x-csrf-token": csrftoken,
},
credentials: "include",// cookie
method: "POST",
mode: "cors",
body: JSON.stringify(json),
})
// .then(res => res.text())
.then(res => res.json())
.then(json => {
// console.log(`text =`, json);
console.log(`json =`, JSON.stringify(json, null, 4));
return json;
})
.catch(err => console.error(`error =`, err));
Promise {<pending>}
VM13893:15 json = {
"id": "123",
"name": "admin"
}
cURL

$ curl 'http://localhost:7001/product/create' \
-H 'Connection: keep-alive' \
-H 'DNT: 1' \
-H 'x-csrf-token: Lg_TzQXsAh7Rk27ztpzl3gYs' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4137.0 Safari/537.36' \
-H 'Content-Type: application/json' \
-H 'Accept: */*' \
-H 'Origin: http://localhost:7001' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Referer: http://localhost:7001/product/create\
-H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7' \
-H 'Cookie: csrfToken=Lg_TzQXsAh7Rk27ztpzl3gYs; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22171319a1aef2f3-088239ecd3d0f5-6a3f0f7b-1764000-171319a1af0b4c%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22url%E7%9A%84domain%E8%A7%A3%E6%9E%90%E5%A4%B1%E8%B4%A5%22%2C%22%24latest_search_keyword%22%3A%22url%E7%9A%84domain%E8%A7%A3%E6%9E%90%E5%A4%B1%E8%B4%A5%22%2C%22%24latest_referrer%22%3A%22url%E7%9A%84domain%E8%A7%A3%E6%9E%90%E5%A4%B1%E8%B4%A5%22%7D%2C%22%24device_id%22%3A%22171319a1aef2f3-088239ecd3d0f5-6a3f0f7b-1764000-171319a1af0b4c%22%7D' \
--data-binary '{"id":"123","name":"admin"}' \
--compressed
Fetch & Headers & CSRF的更多相关文章
- 前后端数据交互(四)——fetch 请求详解
fetch 是 XMLHttpRequest 的升级版,使用js脚本发出网络请求,但是与 XMLHttpRequest 不同的是,fetch 方式使用 Promise,相比 XMLHttpReques ...
- 让你的ASP.NET Core应用程序更安全
让你的ASP.NET Core应用程序更安全 对于ASP.NET Core应用程序,除了提供认证和授权机制来保证服务的安全性,还需要考虑下面的一些安全因素: CSRF 强制HTTPS 安全的HTTP ...
- spring security xml配置详解
security 3.x <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns= ...
- 转:Spring学习笔记---Spring Security登录页
转:http://axuebin.com/blog/2016/06/21/spring-security/?utm_source=tuicool&utm_medium=referral. 提示 ...
- secruity
security3.x <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns=& ...
- Python 爬虫系列
爬虫简介 网络爬虫 爬虫指在使用程序模拟浏览器向服务端发出网络请求,以便获取服务端返回的内容. 但这些内容可能涉及到一些机密信息,所以爬虫领域目前来讲是属于灰色领域,切勿违法犯罪. 爬虫本身作为一门技 ...
- fetch 添加请求头headers
// var headers = new Headers(); // headers.append('Authorization', localStorage.getItem('token')); f ...
- 014_浅说 XSS和CSRF
在 Web 安全领域中,XSS 和 CSRF 是最常见的攻击方式.本文将会简单介绍 XSS 和 CSRF 的攻防问题. 声明:本文的示例仅用于演示相关的攻击原理 XSS XSS,即 Cross Sit ...
- ajax、fetch、axios — 请求数据
jquery ajax jq 的ajax是对原生XHR的封装,除此以外还增添了对JSONP的支持.用起来非常方便 用法: $.ajax({ url:发送请求的地址, data:数据的拼接,//发送到服 ...
随机推荐
- 使用session实现网站N天免登陆()
问题描述: 一些网站的N天之内免登陆实现方式. 方式一: 首先想到的是使用cookie保存用户登录信息,设置有效期,在用户下次访问时免去登录环节,直接通过cookie获取用户信息. 方式二: 方式二: ...
- Java 8教程(知识内容详细,快速学习Java 8)
允许在接口中有默认方法实现 Lambda表达式 函数式接口 方法和构造函数引用 Lambda的范围 内置函数式接口 Predicates Functions Suppliers Consumers C ...
- 周期性清除Spark Streaming流状态的方法
在Spark Streaming程序中,若需要使用有状态的流来统计一些累积性的指标,比如各个商品的PV.简单的代码描述如下,使用mapWithState()算子: val productPvStrea ...
- 通过jenkins构建服务,并发布服务,修改Jenkins以Root用户运行
通过jenkins构建服务,并发布服务,修改Jenkins以Root用户运行 其他博文:从0到1体验Jenkins+Docker+Git+Registry实现CI自动化发布 Jenkins注册中心 一 ...
- Spring Boot项目application.yml文件数据库配置密码加密
在Spring boot开发中,需要在application.yml文件里配置数据库的连接信息,或者在启动时传入数据库密码,如果不加密,传明文,数据库就直接暴露了,相当于"裸奔"了 ...
- shell循环字符串数组
#!/bin/bash arr=("0" "1" "2" "3" "4" "5" ...
- Deep Learning论文翻译(Nature Deep Review)
原论文出处:https://www.nature.com/articles/nature14539 by Yann LeCun, Yoshua Bengio & Geoffrey Hinton ...
- Codeforces Round #646 (Div. 2) C. Game On Leaves(树上博弈)
题目链接:https://codeforces.com/contest/1363/problem/C 题意 有一棵 $n$ 个结点的树,每次只能取叶子结点,判断谁能最先取到结点 $x$ . 题解 除非 ...
- 【noi 2.5_1792】迷宫(bfs 或 dfs)
简单搜索,在n*n的矩阵中,问从起点是否可以到达终点,有些格子不可走,上下左右四个方向都可以走.(N<=100)1.bfs从起点开始走,直到走到终点或全部遍历过一次就结束.2.dfs要一走到终点 ...
- poj3693 Maximum repetition substring (后缀数组+rmq)
Description The repetition number of a string is defined as the maximum number R such that the strin ...