【问题标题】:How to allow set-cookie to be sent in AWS HTTP API Gateway?如何允许在 AWS HTTP API Gateway 中发送 set-cookie?
【发布时间】:2021-09-15 01:43:36
【问题描述】:

注意:这个问题不是关于 AWS REST API Gateway,这个问题是关于 AWS HTTP API Gateway

我的 AWS HTTP API 网关不允许传递 cookie。 我正在使用

  1. 在 ECS 上托管的服务器上的 express.js 应用
  2. 设置 cors 如下:

在 HTTP API 网关上:

在 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


    【解决方案1】:

    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

    【讨论】:

    • 您提供的参考资料和方式适用于 AWS REST API Gateway。我正在寻找 AWS HTTP API Gateway 中的解决方案
    • 我相信它们都以相同的方式工作。 docs.aws.amazon.com/apigateway/latest/developerguide/…
    • aws apigatewayv2 update-api --api-id api-id --cors-configuration AllowOrigins="example.com" 您可以轻松修改此命令以设置允许的标头
    • allow origin 不是我在上面提到的错误中看到的错误
    • 如果有选项请求,您能否发布完整的请求标头和响应标头
    猜你喜欢
    • 2022-10-06
    • 2017-04-09
    • 2017-02-07
    • 2017-11-26
    • 1970-01-01
    • 2016-07-23
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多