【问题标题】:APIGateway CORS enabled and Lambda proxy integration enabled but still running into CORS issuesAPIGateway CORS 已启用且 Lambda 代理集成已启用,但仍遇到 CORS 问题
【发布时间】:2021-10-22 09:21:02
【问题描述】:

我的 API 网关有以下 CDK 代码

    this.restAPI = new RestApi(this, name, {
        cloudWatchRole: true,
        domainName: {
            domainName: props.recordName,
            certificate: props.cert,
            endpointType: EndpointType.REGIONAL,
            securityPolicy: SecurityPolicy.TLS_1_2
        },
        defaultCorsPreflightOptions: {
            allowOrigins: Cors.ALL_ORIGINS,
            allowMethods: ['OPTIONS', 'POST', 'GET', 'DELETE', 'PUT', 'PATCH'],
            allowHeaders: Cors.DEFAULT_HEADERS,
            maxAge: Duration.seconds(86400)
        }
    });

导致这个

但我仍然遇到这个错误:

/#/actors:1 Access to fetch at 'https://web-api.alpha.myapp.com/api/v1/actors?page=1' from origin 'https://alpha.myapp.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

但是,如果您查看我的 Access-Control-Allow-Origin,它设置为“*”。所以我不确定我错过了什么。

只是为了验证我确实打开了 Lambda 代理集成

【问题讨论】:

  • UI 中的值用单引号括起来有什么原因吗?它们是由 CDK 生成的吗?您可以尝试删除它们,然后看看会发生什么?
  • CDK 就是这么干的。

标签: api cors aws-api-gateway aws-cdk


【解决方案1】:

显然,我返回 GET 内容的 lambda 实际上必须包含相关的 CORS 标头。仅在 API Gateway 上开启 CORS 支持是不够的。

【讨论】:

    猜你喜欢
    • 2017-04-29
    • 2018-01-04
    • 2018-07-19
    • 2020-09-05
    • 2014-02-13
    • 2021-03-23
    • 2020-09-26
    • 2019-07-10
    • 2012-09-30
    相关资源
    最近更新 更多