【问题标题】:AWS API Gateway Custom Authorizer with Proxy setup - Add Custom Headers to Request具有代理设置的 AWS API Gateway 自定义授权方 - 将自定义标头添加到请求
【发布时间】:2017-08-30 12:41:13
【问题描述】:

我有什么:

  1. AWS API 网关设置为代理 (/{proxy+})
  2. 一个自定义身份验证函数,用于授权此代理设置的传入请求。
  3. 自定义身份验证函数通过“上下文”对象将我想要传递给请求的附加信息传递给请求,如下所示:

    { “principalId”:“yyyyyyyy”, “政策文件”:{ “版本”:“2012-10-17”, “陈述”: [ { “动作”:“执行 API:调用”, "效果": "允许|拒绝", “资源”:“一些 arn” } ] }, “语境”: { "customInfo1": "你好", “customInfo2”:“世界” } }

我需要什么:

  1. 我需要将在上面的上下文对象中传递的自定义信息传递到自定义标头中,因为它传递给目标函数。

我所知道的:

  1. 如果这不是代理,我可以使用映射模板来获得所需的结果。

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway


    【解决方案1】:

    如果你检查this document,你会发现你可以创建一个自定义的Model来从body映射到header,反之亦然。然后,您可以在 Method Request -> Request Body 下分配此模型。

    【讨论】:

    • 该文档适用于我不使用 Lambda 作为代理集成的情况。我想继续使用它,并简单地从我的自定义授权器中添加一些标题,以防万一。当您在Resources-->Method-->"Integration Request" 中选择 Lambda 代理选项时,映射模板选项消失。
    【解决方案2】:

    想通了,AWS 在配置为代理时将其传递给 Lambda:

    {
        "resource": "/{proxy+}",
        "path": "/echo",
        "httpMethod": "POST",
        "headers": {
            "Accept-Type": "application/json",
            "Authorization": "Bearer xxx",
            "CloudFront-Forwarded-Proto": "https",
            "CloudFront-Is-Desktop-Viewer": "true",
            "CloudFront-Is-Mobile-Viewer": "false",
            "CloudFront-Is-SmartTV-Viewer": "false",
            "CloudFront-Is-Tablet-Viewer": "false",
            "CloudFront-Viewer-Country": "IN",
            "Content-Type": "application/json",
            "Host": "yyy.execute-api.us-east-1.amazonaws.com",
            "User-Agent": "Fiddler",
            "Via": "1.1 aaa.cloudfront.net (CloudFront)",
            "X-Amz-Cf-Id": "uuu",
            "X-Amzn-Trace-Id": "Root=1-58e5w17a-58ff31a846954e0f2aa7cd2c",
            "X-Forwarded-For": "115.112.36.246, 54.182.242.113",
            "X-Forwarded-Port": "443",
            "X-Forwarded-Proto": "https"
        },
        "queryStringParameters": null,
        "pathParameters": {
            "proxy": "echo"
        },
        "stageVariables": null,
        "requestContext": {
            "accountId": "1234567890",
            "resourceId": "1t2w8a",
            "stage": "dev",
            "authorizer": {
                "customKey": "1",
                "eee": "1",
                "principalId": "2",
                "otherkey": "hello",
                "somekey": "1,2"
            },
            "requestId": "qqq",
            "identity": {
                "cognitoIdentityPoolId": null,
                "accountId": null,
                "cognitoIdentityId": null,
                "caller": null,
                "apiKey": null,
                "sourceIp": "aaa.bbb.qq.www",
                "accessKey": null,
                "cognitoAuthenticationType": null,
                "cognitoAuthenticationProvider": null,
                "userArn": null,
                "userAgent": "Fiddler",
                "user": null
            },
            "resourcePath": "/{proxy+}",
            "httpMethod": "POST",
            "apiId": "123"
        },
        "body": "{\"ola\": \"\"}",
        "isBase64Encoded": false
    }
    

    在上面的 requestContext 部分中,我通过自定义授权器传递的所有密钥都已经存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 2016-11-30
      • 1970-01-01
      • 2017-04-29
      • 2019-03-04
      • 2020-06-17
      • 2018-03-08
      相关资源
      最近更新 更多