【发布时间】:2021-09-15 01:43:36
【问题描述】:
注意:这个问题不是关于 AWS REST API Gateway,这个问题是关于 AWS HTTP API Gateway
我的 AWS HTTP API 网关不允许传递 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
【问题讨论】:
标签: node.js amazon-web-services express cookies aws-api-gateway