簡體   English   中英

如何允許在 AWS HTTP API 網關中發送 set-cookie?

[英]How to allow set-cookie to be sent in AWS HTTP API Gateway?

注意:這個問題不是關於 AWS REST API Gateway,這個問題是關於 AWS HTTP API Gateway

我的 AWS HTTP API Gateway 不允許傳遞 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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM