cors - 飞行前响应 403 禁止。如何在没有 x-api-key 的情况下允许选项方法?

标签 cors aws-cloudformation aws-api-gateway preflight sam

我正在使用 SAM 在 cloudformation 中创建我的 API。

我的 options 方法收到了 403 FORBIDDEN(这也是我的 get 方法的预检)。

如何让我的选项方法在没有 x-api-key 的情况下回复 200 OK?

我已经尝试了很多 stackoverflow 答案,但都不适合我的 SAM 模板格式。我已经尝试了AllowHeaders 的所有不同组合。我忽略了 x-api-key - 仍然是相同的 403 FORBIDDEN。

如果我在 postman 中发送我的 x-api-key 和我的请求,我会得到 200 OK,但在我的 ReactJS 应用程序中,它仍然给出与下面相同的错误,我的预检未通过。

控制台记录对 get 方法的响应 enter image description here

postman 对选项方法的响应(预检测试) enter image description here

Cloudwatch 错误 enter image description here

模板.yaml

Globals:
  Function:
    Timeout: 10
  Api:
    Cors:
      AllowMethods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
      AllowHeaders: "'Content-Type,X-Amz-Date,X-Amz-Security-Token,x-api-key,Authorization,Origin,Host,X-Requested-With,Accept,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Access-Control-Allow-Headers'"
      AllowOrigin: "'*'"

Resources:
  BSApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        ApiKeyRequired: true
        AddDefaultAuthorizerToCorsPreflight: false

  GrondvogFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: grondvog/
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Policies:
        - LambdaInvokePolicy: {
          'FunctionName': !Ref RDBQueryFunction
        }
      Environment:
        Variables:
          "RDBQueryFunctionName": !Ref RDBQueryFunction
      Events:
        KryGrondvog:
          Type: Api
          Properties:
            RestApiId: !Ref BSApi
            Path: /grondvog
            Method: get
        OptionsGrondvog:
          Type: Api
          Properties:
            RestApiId: !Ref BSApi
            Path: /grondvog
            Method: options

Lambda函数

if (event.httpMethod == 'OPTIONS') {
        rekords = {
            statusCode: 200,
            headers: {
                "Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,X-Amz-Security-Token,x-api-key,Authorization,Origin,Host,X-Requested-With,Accept,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Access-Control-Allow-Headers",
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT",
                "X-Requested-With": "*"
            },
            body: JSON.stringify({ statusText: "OK" })
        };
    }

最佳答案

好的,我找到了解决方案。

我错过了应该告诉我的 SAM 应用程序对于选项方法我必须指定我不需要 api key 的部分。我刚刚将以下内容添加到每个选项方法中:

Auth:
  ApiKeyRequired: false
GrondvogFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: grondvog/
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Policies:
        - LambdaInvokePolicy: {
          'FunctionName': !Ref RDBQueryFunction
        }
      Environment:
        Variables:
          "RDBQueryFunctionName": !Ref RDBQueryFunction
      Events:
        KryGrondvog:
          Type: Api
          Properties:
            RestApiId: !Ref BSApi
            Path: /grondvog
            Method: get
        OptionsGrondvog:
          Type: Api
          Properties:
            RestApiId: !Ref BSApi
            Path: /grondvog
            Method: options
            Auth:
              ApiKeyRequired: false

关于cors - 飞行前响应 403 禁止。如何在没有 x-api-key 的情况下允许选项方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60899237/

相关文章:

javascript - axios onUploadProgress 和 onDownloadProgress 不适用于 CORS

javascript - 在 Google App Engine 服务器中实现 CORS

cordova - Meteor cordova 不允许 CORS

amazon-web-services - 不支持的属性*密码

javascript - AWS API Gateway 未检测到 Angular $resource POST 请求查询参数?

jquery - 尽管可以使用 CURL 进行 POST,但使用 CORS 到 API 网关的 AJAX POST 不起作用

python - 将 Docker 容器用于前端和后端时,CORS 失败

aws-cloudformation - 如何使用CDK修改EKS的默认容量实例角色?

amazon-web-services - 引用 SAM 模板 (YAML) 中的元数据

node.js - AWS API网关WebSocket API : how to use it with React/NodeJS/native WebSocket?