【问题标题】:How to configure CORS for an AWS API Gateway Custom Authorizer?如何为 AWS API Gateway 自定义授权方配置 CORS?
【发布时间】:2018-03-08 11:16:10
【问题描述】:

我有一个由 API Gateway 和 Lambda 提供支持的 API,它使用自定义授权方。

对于成功的请求,它会通过授权方,然后我的 Lambda 可以毫无问题地返回带有 CORS 标头的正确响应。

但是,对于不成功的授权(例如无效令牌),我没有得到 CORS 标头,这会导致我的客户端应用程序(使用 fetch API)抛出。

如何为使用自定义授权方的 API 设置 CORS?

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway serverless-framework


    【解决方案1】:

    根据this answerthis AWS documentation page,我能够弄清楚如何解决它。

    解决方法是在我的serverless.yml中添加以下内容:

    resources:
      Resources:
        AuthorizerApiGatewayResponse:
          Type: "AWS::ApiGateway::GatewayResponse"
          Properties:
            ResponseParameters:
              "gatewayresponse.header.Access-Control-Allow-Origin": "'*'"
            ResponseType: UNAUTHORIZED
            RestApiId: {"Ref" : "ApiGatewayRestApi"}
            StatusCode: "401"
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 2019-03-04
    • 2018-04-17
    • 2018-03-21
    • 2020-06-17
    • 2018-03-09
    • 2019-09-06
    • 1970-01-01
    相关资源
    最近更新 更多