在 AWS Lambda 上配置 CORS 响应标头? [英] Configure CORS response headers on AWS Lambda?

查看:39
本文介绍了在 AWS Lambda 上配置 CORS 响应标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AWS API Gateway 创建一个新服务,但我发现浏览器会自动调用 OPTIONS 方法以获取 CORS 信息.

问题在于 AWS API Gateway 不提供配置 CORS 标头的本机方式.

是否可以创建一个 Lambda 脚本来响应 OPTIONS 方法?

解决方案

如果您使用 {proxy+} 端点,则必须在 Lambda 函数中处理 CORS HTTP 请求.实现取决于您使用的框架.对于 Express,最简单的解决方案是简单地使用 Express CORS 中间件..>

如果您不想通过 Lambda 处理 CORS 请求,请尝试更改您的 Lambda 方法 的设置以处理 API 网关 级别的 CORS.

这是CORS 设置的详细官方教程AWS API 网关.

Access-Control-Allow-Headers 中允许标题 X-Api-Key 也很重要,否则 auth 将不起作用,你会得到错误.

编辑:2015 年 11 月,API Gateway 团队添加了一项新功能以简化 CORS 设置.

I'm trying to create a new service using AWS API Gateway, but I found out the browser automatically calls OPTIONS method in order to obtain CORS information.

The problem is that AWS API Gateway does not offer a native way to configure CORS headers.

Is it possible to create a Lambda Script in order to respond to OPTIONS method?

解决方案

If you're using {proxy+} endpoint, you must handle CORS HTTP requests in the Lambda function. The implementation depends on the framework you're using. For Express, the easiest solution is to simply use Express CORS middleware.

If you don't want to handle CORS requests by Lambda, try changing the settings of your Lambda Method to handle CORS on the API Gateway level.

Here's a detailed official tutorial for CORS setup on AWS API Gateway.

It's also critical that you allow header X-Api-Key in Access-Control-Allow-Headers otherwise auth won't work and you'll get errors.

EDIT: In November 2015 the API Gateway team added a new feature to simplify CORS setup.

这篇关于在 AWS Lambda 上配置 CORS 响应标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆