0

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

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

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

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

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

控制台日志响应 get 方法 在此处输入图像描述

邮递员对选项方法的响应(飞行前测试) 在此处输入图像描述

Cloudwatch 错误 在此处输入图像描述

模板.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" })
        };
    }
4

1 回答 1

1

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

我错过了应该告诉我的 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
于 2020-03-30T07:08:40.723 回答