具有代理设置的AWS API网关自定义授权程序 - 将自定义标头添加到请求

时间:2017-04-05 04:49:19

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

我有什么:

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

    {   “principalId”:“yyyyyyyy”,   “policyDocument”:{     “版本”:“2012-10-17”,     “声明”:[       {         “行动”:“执行-api:调用”,         “效果”:“允许|拒绝”,         “资源”:“一些人”       }     ]   },   “上下文”:{     “customInfo1”:“你好”,     “customInfo2”:“世界”   } }

  4. 我需要什么:

    1. 我需要将上面的上下文对象中传递的自定义信息传递给请求,因为它传递给目标函数。
    2. 我所知道的:

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

2 个答案:

答案 0 :(得分:2)

如果选中this document,您会发现可以创建自定义模型以从正文映射到标题,反之亦然。然后,您可以在方法请求 - >下分配此模型请求正文

答案 1 :(得分: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部分中,我通过自定义授权程序传递的所有密钥都已存在。