禁止预检响应403。没有x-api-key的情况下如何允许选项方法? [英] Preflight response 403 forbidden. How can I allow options method without x-api-key?

查看:199
本文介绍了禁止预检响应403。没有x-api-key的情况下如何允许选项方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我在options方法上获得了403 FORBIDDEN(因此我的get方法也处于预检阶段)。



如何在没有x-api-key的情况下允许我的options方法以200 OK进行回复?



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



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



对get方法



邮递员对选项方法的响应(飞行前测试)



模板.yaml

 全局变量:
功能:
超时:10
Api:
Cors:
AllowMethods:'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'
AllowHeaders:'Content-Type, X-Amz日期,X-Amz安全令牌,x-api密钥,授权,来源,主机,X请求的方式,接受,访问控制允许方法,访问控制允许来源, Access-Control-Allow-Headers'
AllowOrigin:'*'

资源:
BSApi:
类型:AWS :: Serverless :: Api
属性:
StageName:产品
身份验证:
ApiKeyRequired:true
AddDefaultAuthorizerToCorsPreflight:false

GrondvogFunction:
类型:AWS: :Serverless :: Function
属性:
CodeUri:grondvog /
处理程序:app.lambdaHandler
运行时:nodejs12.x
策略:
-LambdaInvokePolicy: {
'FunctionName':!Ref RDBQueryFunction
}
环境:
变量:
RDBQueryFunctionName:!Ref RDBQue ryFunction
事件:
KryGrondvog:
类型:Api
属性:
RestApiId:!Ref BSApi
路径:/ grondvog
方法:get
选项Grondvog:
类型:Api
属性:
RestApiId:!Ref BSApi
路径:/ grondvog
方法:选项

Lambda函数

  if( event.httpMethod =='OPTIONS'){
rekords = {
statusCode:200,
标头:{
Access-Control-Allow-Headers:内容类型,X-Amz日期,X-Amz安全令牌,x-api密钥,授权,来源,主机,X请求方式,接受,访问控制允许方法,访问控制允许来源,Access-Control-Allow-Headers,
Access-Control-Allow-Origin: *,
Access-Control-Allow-Methods:删除,获取,打印头,选项,PATCH,POST,PUT,
X-Requested-With: *
},
正文:JSON.stringify({statusText: OK})
};
}


解决方案

好的,我找到了解决方案



我错过了告诉我的SAM应用程序的那一部分,对于options方法,我必须指定我不需要api键。我只是在每个选项方法中添加了以下内容:

  Auth:
ApiKeyRequired:false



  Grondvog函数:
类型:AWS :: Serverless :: Function
属性:
CodeUri:grondvog /
处理程序:app.lambdaHandler
运行时:nodejs12.x
策略:
-LambdaInvokePolicy:{
'FunctionName': !Ref RDBQueryFunction
}
环境:
变量:
RDBQueryFunctionName:!Ref RDBQueryFunction
事件:
KryGrondvog:
类型:Api
属性:
RestApiId:!Ref BSApi
路径:/ grondvog
方法:get
OptionsGrondvog:
类型:Api
属性:
RestApiId:!Ref BSApi
路径:/ grondvog
方法:options
Auth:
Api密钥必填:false


I'm using SAM to create my API in cloudformation.

I'm getting a 403 FORBIDDEN on my options method (thus also my preflight for my get method).

How can I allow my options method to reply with 200 OK without my x-api-key?

I've tried so many stackoverflow answers but none fit my SAM template format. I've tried all the different combinations of my AllowHeaders. I've ommited the x-api-key - still the same 403 FORBIDDEN.

If I send my x-api-key in postman with my request I get a 200 OK, but in my reactjs application it still gives the same error as below that my preflight does not pass.

Console log response to the get method

Postman response to the options method (preflight testing)

Cloudwatch error

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 function

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" })
        };
    }

解决方案

Alright, I found the solution.

I missed the part where I should tell my SAM application that for the options method I must specify that I don't want the api key. I just added the following to each options method:

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

这篇关于禁止预检响应403。没有x-api-key的情况下如何允许选项方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆