localhost - SAM Local 似乎没有运行授权程序功能

标签 localhost aws-sam-cli aws-sam

我刚刚开始使用 SAM Local ,但在尝试为我的端点配置授权器函数时再次出现问题。

我一直在查看 main SAM documentation 以了解如何设置 Auth 函数,但是每当我尝试使用 sam local start-api 在本地运行 API 时,它运行良好,但好像它甚至没有尝试运行 auth 函数。

我已经尝试在 Global.API 中定义 Auth 以及在 SAM 的 template.yaml 的资源部分中定义 API 资源

# template.yaml
Globals:
  Function:
    Timeout: 3
    CodeUri: src/
    Runtime: nodejs8.10
  Api:
    Auth:                        # Option #1: Defining it globally
      DefaultAuthorizer: CustomJWTAuthorizer
      Authorizers:
        CustomJWTAuthorizer:
          FunctionArn: !GetAtt AuthFunction.Arn    
Resources:
  UserApi:
    Auth:                        # Option #2: Defining it as an API resource
      Authorizers:
        MyLambdaTokenAuth:
          FunctionPayloadType: TOKEN
          FunctionArn: !GetAtt AuthFunction.Arn
      DefaultAuthorizer: MyLambdaTokenAuth
  GetUserFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: handler.getUser
      Events:
        GetUser:
          Type: Api
          Properties:
            Path: /users/{userId}
            Method: get
            Auth:                    # Option #3: Define it on the function level
              Authorizer: AuthFunction
            RestApiId:
                Ref: UserApi
  AuthFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: handler.authorize

我已经尝试将事件打印到控制台,并且可以看到 event.requestContext 只是填充了虚拟数据,而不是在实时推送时传递它:
  // console.log(event)

  ...  
  resource: '/users/{userId}', 
  requestContext:     { resourceId: '123456',
     apiId: '1234567890',
     resourcePath: '/users/{userId}',
     httpMethod: 'GET',
     requestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
     accountId: '123456789012',
     stage: null,
     identity: 
      { apiKey: null,
        userArn: null,
        cognitoAuthenticationType: null,
        caller: null,
        userAgent: 'Custom User Agent String',
        user: null,
        cognitoIdentityPoolId: null,
        cognitoAuthenticationProvider: null,
        sourceIp: '127.0.0.1',
        accountId: null },
   extendedRequestId: null,
   path: '/users/{userId}' },
   ...

最佳答案

不幸的是,在本地运行代码时,AWS SAM CLI 尚不支持授权方。但是有一个开放的功能请求来添加对它的支持:https://github.com/awslabs/aws-sam-cli/issues/137 .

关于localhost - SAM Local 似乎没有运行授权程序功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57632539/

相关文章:

node.js - 在本地主机上使用 client.html 聊天并在线运行 server.js?

php - 从本地主机发送电子邮件

ssh - 错误启动 "XAMPP"堆栈 : cannot start stack: ssh not accessible

amazon-web-services - 如何使用 Dockerized SAM Local 设置调试

amazon-web-services - 尽管 SAM 模板中提到了 S3 存储桶,但为什么没有创建它?

amazon-web-services - AWS SAM 本地 Lambda 调用缓慢

mysql - 无法连接我的主机中的 mysql 数据库

aws-lambda - aws sam 发布/部署流程

aws-lambda - AWS lambda SAM 部署错误 - 模板格式错误 : Unresolved resource dependencies

aws-lambda - 将 Lambda 日志写入 CloudWatch 时出现问题,可能是什么问题?