如何在自定义授权方 AWS lambda 函数中访问 http 标头

Posted

技术标签:

【中文标题】如何在自定义授权方 AWS lambda 函数中访问 http 标头【英文标题】:How to access http headers in custom authorizer AWS lambda function 【发布时间】:2016-11-30 11:37:54 【问题描述】:

在 API Gateway 中,我使用 Python 中的 Lambda 函数为我的 API 创建了一个 custom authorizer。 API Gateway 使用我配置的标头 (method.request.header.Authorization) 移交传入的身份验证令牌。但是,我还需要 lambda 函数中原始 http 请求的其他标头。我如何访问它们?我没有看到 event 对象输入到我的 lambda 函数的标题。

请注意,这不是 How to access HTTP headers for request to AWS API Gateway using Lambda? 的副本。问题是关于自定义授权器 lambda 函数。我没有看到任何将传入的 http 标头传递给授权 lambda 函数的配置选项。

根据AWS Documentation,API 网关使用以下输入调用自定义授权者。基于以下内容,我认为我的要求是不可能的。但想检查是否有解决方法。

“类型”:“令牌”, "授权令牌":"", "methodArn":"arn:aws:execute-api:::///"

【问题讨论】:

不可能。为什么需要多个标题?解决方法:在集成的 Lambda 函数中自行处理身份验证/授权。 【参考方案1】:

这现在可以通过使用“请求”类型的授权者而不是令牌来实现

完整的细节在这里: https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html

从根本上说,所有标头都在请求授权的事件对象中传递

ie headers 事件上的对象


    "headers": 
        "X-wibble": "111",
        "X-wobble": "222",
        "x-amzn-ssl-client-hello": "*Deleted*",
        "Via": "1.1 .cloudfront.net (CloudFront)",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Forwarded-Proto": "https",
        "X-Forwarded-For": "*Deleted*",
        "CloudFront-Viewer-Country": "GB",
        "Accept": "*/*",
        "User-Agent": "curl/7.55.1",
        "X-Amzn-Trace-Id": "Root=*Deleted*",
        "Host": "*Deleted*.execute-api.eu-west-1.amazonaws.com",
        "X-Forwarded-Proto": "https",
        "X-Amz-Cf-Id": "*Deleted*",
        "CloudFront-Is-Tablet-Viewer": "false",
        "X-Forwarded-Port": "443",
        "CloudFront-Is-Mobile-Viewer": "false"
    

【讨论】:

【参考方案2】:

这是一个 SAM 模板:

ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        Authorizers:
          MyAuthorizer:
            FunctionPayloadType: REQUEST
            FunctionArn: !GetAtt AuthLambda.Arn
            Identity:
              Headers:
                - X-API-KEY
                - X-API-ID

【讨论】:

【参考方案3】:

有几种方法可以做到。

    您可以定义 SAM 模板(API 网关),在 headers 下,您可以定义多个 headers,您可以在应用程序中检索它们。

    在请求中,可以获取多个customheaders

    "headers": 
          "Access-Control-Allow-Origin": 
            "type": "string",
            "description": "URI that may access the resource"
          ,
          "Access-Control-Allow-Methods": 
            "type": "string",
            "description": "Method or methods allowed when accessing the resource"
          ,
          "Access-Control-Allow-Headers": 
            "type": "string",
            "description": "Used in response to a preflight request to indicate which HTTP headers can be used when making the request."
          
        
    

关注link 会有所帮助

【讨论】:

以上是关于如何在自定义授权方 AWS lambda 函数中访问 http 标头的主要内容,如果未能解决你的问题,请参考以下文章

AWS API Gateway 自定义授权方给予例外

如何从 AWS API Gateway 自定义授权方检索 Spring Boot 中的上下文对象?

如何在 API 网关上的 cognito 授权方保护的 lambda 函数中获取 AWS Cognito 用户数据

从自定义授权方 Lambda 函数访问 POST 请求正文

AWS 自定义授权方 - 从 cookie 获取令牌

如何为 AWS API Gateway 自定义授权方配置 CORS?