gpt4 book ai didi

amazon-web-services - 如何为 AWS API Gateway 自定义授权方配置 CORS?

转载 作者:行者123 更新时间:2023-12-05 00:50:03 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

关于amazon-web-services - 如何为 AWS API Gateway 自定义授权方配置 CORS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46439875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com