我的设置看起来像这样。
|––––––––––––| |–––––––––––––| |–––––––––––––––––|
| | <- origin 1 -> | API Gateway | <-> | Lambda function |
| | |–––––––––––––| |–––––––––––––––––|
| CloudFront |
| | |–––––––––––––|
| | <- origin 2 -> | S3 bucket |
|––––––––––––| |–––––––––––––|
我需要在API网关前的CloudFront来获得自动的http->https重定向。
我正在使用一个自定义的login.example.com
子域和CloudFront。
API Gateway生成的URL是CloudFront分发的起源1。
这一切都像预期的那样运作。
我甚至可以从lambda函数中返回一个Set-Cookie
头,它将被传递下去,直到到达浏览器。
"statusCode": 302,
"body": "",
"headers": {
"location": "/test",
"surrogate-control": "no-store",
"cache-control": "no-store, no-cache, must-revalidate, proxy-revalidate",
"pragma": "no-cache",
"expires": "0",
"content-length": "0",
"date": "Fri, 19 Feb 2021 17:25:56 GMT",
"connection": "keep-alive",
"set-cookie": "cookie1=68abcdbefbef7d84c26e68; Max-Age=2592000; Domain=example.com; Path=/; HttpOnly; Secure; SameSite=Strict"
"isBase64Encoded": false
再加一个就不行了--当你看了文档后就会发现。
https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#apigateway-multivalue-headers-and-parameters
https://aws.amazon.com/blogs/compute/support-for-multi-value-parameters-in-amazon-api-gateway/
"statusCode": 302,
"headers": {
"location": "/test",
"set-cookie": [
"cookie1=68abcdbefbef7d84c26e68; Max-Age=2592000; Domain=example.com; Path=/; HttpOnly; Secure; SameSite=Strict",
"cookie2-login=; Max-Age=0; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure"
"surrogate-control": "no-store",
"cache-control": "no-store, no-cache, must-revalidate, proxy-revalidate",
"pragma": "no-cache",
"expires": "0",
"content-length": "0"
这两种情况都将被忽略/删除。
但是,即使我使用multiValueHeaders
对象来返回一个以上的同类对象,像这样。
"statusCode": 302,
"body": "",
"headers": {
"location": "/test",
"surrogate-control": "no-store",
"cache-control": "no-store, no-cache, must-revalidate, proxy-revalidate",
"pragma": "no-cache",
"expires": "0",
"content-length": "0",
"date": "Fri, 19 Feb 2021 17:25:56 GMT",
"connection": "keep-alive"
"isBase64Encoded": false,
"multiValueHeaders": {
"Set-Cookie": [
"cookie1=68abcdbefbef7d84c26e68; Max-Age=2592000; Domain=example.com; Path=/; HttpOnly; Secure; SameSite=Strict",
"cookie2-login=; Max-Age=0; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure"
API Gateway会从它传递给CloudFront的响应中删除/忽略它们。
我做错了什么?
在使用multiValueHeaders
时,我是否必须在API网关中映射一些东西?
正常的headers['set-cookie']
会自动传递,但multiValueHeaders
却没有?
额外的属性是一个问题吗?
我试图为根域而不是login.example.com域设置cookie,这是不是一个问题?
终于自己找到了答案。
新的有效载荷格式(2.0)不支持
。
multiValueHeaders
与AWS Lambda代理集成的HTTP API工作
[...] Format 2.0没有multiValueHeaders或multiValueQueryStringParameters字段。重复的标题用逗号组合,并包含在标题字段中。重复的查询字符串用逗号组合,并包括在queryStringParameters字段中。[...]
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
所以我把
头像改写成不同的拼法。
set-cookie