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

Preflight response 403 forbidden. How can I allow options method without x-api-key?

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

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

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

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

如果我根据请求在邮递员中发送我的 x-api-key,我会得到 200 OK,但在我的 reactjs 应用程序中它仍然给出与下面相同的错误,我的预检没有通过。

控制台记录对 get 方法的响应

邮递员响应选项方法(预检测试)

Cloudwatch 错误

template.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 键的选项方法的部分。我刚刚将以下内容添加到每个选项方法中:

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