代码之家  ›  专栏  ›  技术社区  ›  Noel Llevares

如何为AWS API网关自定义授权器配置CORS?

  •  6
  • Noel Llevares  · 技术社区  · 8 年前

    我有一个由API网关和Lambda支持的API,它使用自定义授权器。

    对于成功的请求,它通过授权者,然后我的Lambda可以返回正确的响应,并带有CORS头,没有问题。

    然而,对于不成功的授权(例如无效令牌),我没有得到CORS头,这导致我的客户端应用程序(使用 fetch

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

    1 回复  |  直到 8 年前
        1
  •  5
  •   ceilfors    7 年前

    基于 this answer this 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"