API网关选项方法抛出403

Gee*_*ets 5 amazon-web-services aws-lambda aws-api-gateway aws-sam

我有一个Custom AuthorizerAPI Gateway. SAM Module当通过它部署Options Method时,当您启用CORS. 我真正不明白的是为什么自定义授权者会附加到Options端点?在此输入图像描述

403当我尝试从浏览器调用端点时,会抛出此异常,而当我AuthorizationOptions方法中删除时,此异常会正常工作。

在此输入图像描述

下面是template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Globals:
  Function:
    Runtime: nodejs8.10
  Api:
    Cors:
      AllowMethods: "'*'"
      AllowHeaders: "'*'"
      AllowOrigin: "'*'"

Resources:
  TestApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      Auth:
        DefaultAuthorizer: testAuthoriser
        Authorizers:
          testAuthoriser:
            FunctionArn:
              Fn::ImportValue: !Sub test-custom-autoriser
            Identity:
              Header: Authorization
              ValidationExpression: ^Bearer [-0-9a-zA-Z\._]*$
              ReauthorizeEvery: 30 

  Version:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: test
      CodeUri: src/test
      Handler: index.test
      Events:
        EndPoint:
          Type: Api
          Properties:
            RestApiId: !Ref TestApi
            Path: /test
            Method: get
            Auth:
              Authorizer: testAuthoriser
Run Code Online (Sandbox Code Playgroud)

'Access-Control-Allow-Origin': '*'我也启用了in 标头。不知道这里发生了什么。任何帮助,将不胜感激

you*_*jin 5

这是答案,请参阅此处的aws sam 问题

 Api:
    Cors:
      AllowHeaders: "'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization'" 
      AllowOrigin: "'*'"
    Auth:
      DefaultAuthorizer: CognitoAuthorizer
      Authorizers:
        CognitoAuthorizer:
          UserPoolArn: yourUserPool
      AddDefaultAuthorizerToCorsPreflight: False // <== this line
Run Code Online (Sandbox Code Playgroud)


A.K*_*han 0

对于 CORS,AWS API Gateway 将始终启用 OPTIONS 方法以允许预检测试。您可以在文档中阅读更多相关内容

您在浏览器中看到预检错误的原因是 403 Forbidden 来自您的自定义授权者。自定义授权者不会返回标头,因此如果请求被自定义授权者拒绝,您将始终看到预检错误。

要对此进行调试,请记录您的自定义授权者返回的策略。然后您可以在 CloudWatch 中看到这一点。策略必须包含所请求资源的允许语句。