简体   繁体   English

CORS 在 AWS 上不可能 Lambda HTTP API 网关集成

[英]CORS impossible on AWS Lambda HTTP API Gateway Integration

An AWS Lamba function (NodeJS) returning 3 HTTP headers: aaa, Access-Control-Allow-Origin and bbb was created:返回 3 HTTP 标头的 AWS Lamba function (NodeJS):aaa、Access-Control-Allow-Origin 和 bbb 已创建:

exports.handler = async (event) => {
    const response = {
        statusCode: 200,
        headers: { "aaa":"aaa", "Access-Control-Allow-Origin":"*", "bbb":"bbb" },
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

The function is integrated into a HTTP API (not REST API). function 集成到 HTTP API(不是 REST API)中。 In the HTTP API Gateway Configuration, Section "Configure CORS", the HTTP header "Access-Control-Allow-Origin" was set to "*".在 HTTP API 网关配置的“配置 CORS”部分中,HTTP header“Access-Control-Allow-Origin”设置为“*”。 Please see the screenshot:请看截图:

Gateway Config网关配置

The command "curl -i https://xxxxxxxxxx.execute-api.eu-central-1.amazonaws.com " proves that the HTTP Header Access-Control-Allow-Origin is explicitly removed, because only HTTP headers aaa and bbb are returned:命令“curl -i https://xxxxxxxxxx.execute-api.eu-central-1.amazonaws.com ”证明HTTP Header Access-Control-Allow-Origin被显式删除,因为只有8827167211588和header bbbs是a回来:

HTTP/2 200 
date: Tue, 14 Apr 2020 11:01:58 GMT
content-type: text/plain; charset=utf-8
content-length: 20
aaa: aaa
bbb: bbb
apigw-requestid: K-S2EjVWliAEJKw=

Why on earth is this header still not present, even after "Configure CORS" was done?为什么这个 header 仍然不存在,即使在“配置 CORS”完成之后?

(I'm googling now for more than two days in order to find a solution and it makes me go nuts) (为了找到解决方案,我现在谷歌搜索了两天多,这让我 go 发疯了)

As per Configuring CORS for an HTTP API -根据为 HTTP API 配置 CORS -

If you configure CORS for an API, API Gateway ignores CORS headers returned from your backend integration.如果您为 API 配置 CORS,则 API 网关将忽略从您的后端集成返回的 Z5A8FEFF0B4BDE3EEC9244B76023B79 标头。

That's why the CORS headers from your Lambda (integration) are being ignored.这就是您的 Lambda(集成)中的 CORS 标头被忽略的原因。 This is one of the differences between the new HTTP APIs from the original REST APIs.这是新的 HTTP API 与原始 REST API 之间的区别之一。 In case of these APIs -如果是这些 API -

For a CORS request, API Gateway adds the configured CORS headers to the response from an integration.对于 CORS 请求,API 网关将配置的 CORS 标头添加到来自集成的响应中。

When you do a simple curl, that is not actual doing a cross-origin request.当您执行简单的 curl 时,这并不是实际执行跨域请求。 Hence, you don't see the CORS headers that would be set by the HTTP API.因此,您看不到由 HTTP API 设置的 CORS 标头。 To verify if a CORS request works, I passed an Origin header in the below request and I can see the CORS headers along with my custom headers from Lambda - To verify if a CORS request works, I passed an Origin header in the below request and I can see the CORS headers along with my custom headers from Lambda -

$ curl -v -X GET https://$API_ID.execute-api.$AWS_REGION.amazonaws.com -H "Origin: https://www.example.com"

< HTTP/2 200
< date: Tue, 14 Apr 2020 18:02:26 GMT
< content-type: text/plain; charset=utf-8
< content-length: 18
< aaa: aaa
< bbb: bbb
< access-control-allow-origin: https://www.example.com
< access-control-expose-headers: date, x-api-id

Below is a snippet of my CORS configuration on the API.下面是我在 API 上的 CORS 配置的片段。 I added Access-Control-Allow-Origin value as https://www.example.com and passed this as a part of the Origin header in my curl request. I added Access-Control-Allow-Origin value as https://www.example.com and passed this as a part of the Origin header in my curl request. Such a request would qualify as CORS.这样的请求将符合 CORS 的条件。

在此处输入图像描述

For POST/PUT requests, you'll need to white list the content-type header.对于 POST/PUT 请求,您需要将content-type header 列入白名单。 Putting the wildcard doesn't do the trick for some reason, you need to explicitly whitelist it.由于某种原因,放置通配符并不能解决问题,您需要明确地将其列入白名单。

apigw 配置

For Googlers:对于 Google 员工:

If your OPTIONS preflights succeed but no Access-Control- headers present, and if you are testing using CURL, take extra attention to spell required headers for a preflight:如果您的OPTIONS预检成功但没有Access-Control-标头存在,并且您正在使用 CURL 进行测试,请特别注意拼写预检所需的标头:

  • Access-Control-Request-Method: GET访问控制请求方法:GET
  • Access-Control-Request-Headers: authorization访问控制请求标头:授权
  • Origin: https://theaws.blog产地: https://theaws.blog

If wrongly spelt, OPTIONS succeeds with 204 No Content , but no Access-Control- headers thus render the preflight result invalid.如果拼写错误, OPTIONS会以204 No Content成功,但没有Access-Control-标头因此会使预检结果无效。

Also check you have enough scope for the parameters, as long as your requested method and headers are a subset of the parameter, you will get Access-Control- headers.还要检查是否有足够的 scope 用于参数,只要您请求的方法和标头是参数的子集,您将获得Access-Control-标头。 If not, you will get 204 No Content instead, which I would say it's not very informative!如果没有,你会得到204 No Content ,我想说这不是很丰富!

From the configuration doc :从配置文档

If you configure CORS for an API, API Gateway ignores CORS headers returned from your backend integration如果为 API 配置 CORS,API 网关会忽略从后端集成返回的 Z5A8FEFF0B4BDE3EEC9244B76023B79 标头

Removing CORS from AWS actually fixed this for me.从 AWS 中删除 CORS 实际上为我解决了这个问题。 Even though I had everything setup properly there, it was for some reason explicitly removing access-control-* headers from my response calls.即使我在那里正确设置了所有内容,但出于某种原因,还是从我的响应调用中明确删除了 access-control-* 标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM