node.js - 如何允许在 AWS HTTP API Gateway 中发送 set-cookie?
问题描述
注意:这个问题不是关于 AWS REST API Gateway,这个问题是关于 AWS HTTP API Gateway
我的 AWS HTTP API Gateway 不允许传递 cookie。我在用
- 托管在 ECS 上的服务器上的 express.js 应用程序
- 设置 cors 如下:
在 express.js 服务器上,我通过以下方式配置了 cors:在 app.js 中
const cors = require("cors");
app.use(cors({
credentials: true
}));
对请求的响应以下列方式发送:
const options = {
maxAge: 900000,
httpOnly: true,
secure: true,
sameSite: 'none'
};
res.status(200)
.cookie("accessToken", accessToken, options)
.json({});
当我从客户端和服务器之间删除 HTTP API 网关时,客户端正在正确接收 cookie。但是当调用 API Gateway 时,响应会抛出以下错误:
**Access to fetch at 'https://api.*****.**/login' from origin 'https://cookie.*****.**' has been blocked by CORS policy: Request header field custom_field_name is not allowed by Access-Control-Allow-Headers in preflight response.**
^ 如何解决此错误?
这是来自开发者控制台 > 网络的预检请求及其响应
一般的
Request URL: https://api.*****.**/login
Request Method: OPTIONS
Status Code: 204
Remote Address: [64:ff9b::306:a6f7]:443
Referrer Policy: strict-origin-when-cross-origin
响应头
access-control-allow-headers: *
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD, OPTIONS
Access-Control-Allow-Origin: https://cookie.*****.**
access-control-max-age: 0
apigw-requestid: CIlgSiwhBcwEJyQ=
date: Thu, 08 Jul 2021 04:45:02 GMT
请求标头
:authority: api.*****.**
:method: OPTIONS
:path: /login
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
access-control-request-headers: custom_field_name,content-type
access-control-request-method: POST
origin: https://cookie.*****.**
referer: https://cookie.*****.**/
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-site
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
解决方案
https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors-console.html
阅读关于 Access-Control-Allow-Headers 的第 5 点
该字段包含您可以通过的允许标头列表。您只需将 set-cookie 添加到该列表
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
推荐阅读
- python-2.7 - 在 pycrypto 中使用 32 字节的初始化向量
- javascript - Webpack更改css文件中的url
- python - 使用 OCR 从图像中提取某些文本
- gpu - 如何释放 GPU 上的空间?
- vue.js - 如何禁用 VueStrap 日期选择器
- python - Python,将浮点数转换为整数
- angular - 如何关闭 Angular 6 ng 测试的源映射?
- web-scraping - 通过使用简单的 dom 解析器向其他网站发送输入来获取数据
- java - 支持六边形/端口和适配器架构的 JEE 服务器
- php - 如何插入 echo 语句以插入 RDFa Lite 标记