2

我有一个由API Gateway和Lambda支持的API,它使用一个自定义授权者。如何为AWS API网关自定义授权人配置CORS?

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

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

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

回答

1

根据this answer和​​,我能够弄清楚如何解决它。

的解决方案是增加在下面我serverless.yml

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