【问题标题】:API Gateway HTTP CORSAPI 网关 HTTP CORS
【发布时间】:2020-07-16 01:56:17
【问题描述】:

我有一个后端,它在mydomain.com 公开一个普通的 http 服务器。我想要一个安全的端点,所以我决定创建一个 API Gateway 端点,将流量重定向到我的域端点。

这样做时,我会丢失后端公开的所有 CORS 标头。因此,如果直接向后端发送请求,我会得到以下包含 CORS 标头的响应:

HTTP/1.1 200 OK
Access-Control-Allow-Methods: OPTIONS, POST, GET
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 300
Connection: keep-alive
Date: Fri, 03 Apr 2020 20:03:03 GMT
Transfer-Encoding: chunked
foo: bar

Hello World

但是当我向 API Gateway 端点请求时,我得到以下响应:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 11
Content-Type: text/plain; charset=utf-8
Date: Fri, 03 Apr 2020 20:03:09 GMT
apigw-requestid: KbRzrjEvjoEEMHQ=
foo: bar

我尝试在 API Gateway Cors 面板中设置这些 CORS 标头,但没有成功。

这是我设置 CORS 标头时后端的部分。

app.addListener('request', (req, res) => {
  const headers = {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
    'Access-Control-Max-Age': 300, // 30 days
    'foo': 'bar'
    /** add other headers as per requirement */
  };

  if (req.method === 'OPTIONS') {
    res.writeHead(204, headers);
    res.end();
    return;
  }

  if (['GET', 'POST'].indexOf(req.method) > -1) {
    res.writeHead(200, headers);
    res.end('Hello World');
    return;
  }

  res.writeHead(405, headers);
  res.end(`${req.method} is not allowed for the request.`);
})

我该如何解决这个问题?

【问题讨论】:

  • 您是使用代理还是非代理 HTTP 与 API Gateway 集成?
  • 我也有同样的问题。 stackoverflow.com/questions/61215324/… 如果我找到解决方法,我会发表评论。与 AWS 支持联系。我也将这个问题发送给他们。

标签: amazon-web-services ssl cors aws-api-gateway


【解决方案1】:

我之前也遇到过类似的问题;

如果响应是通过代理集成发送的,它将保留它。

否则,需要在 APIGateway 中配置 CORS 标头。在下拉菜单中,您可以“启用 CORS”并设置所需的标头,或将其设置为代理集成。

【讨论】:

    【解决方案2】:

    使用代理集成或启用 CORS。

    代理:

    CORS:

    【讨论】:

      猜你喜欢
      • 2020-07-04
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 2016-05-15
      • 2016-05-18
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多