首页 文章

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

提问于
浏览
3

What I have:

  • AWS API网关设置为代理(/

  • 自定义Auth功能,用于授权此代理设置的传入请求 .

  • 自定义身份验证功能通过“上下文”对象传递我想传递给请求的其他信息,如下所示:

{“principalId”:“yyyyyyyy”,“policyDocument”:{“Version”:“2012-10-17”,“Statement”:[{“Action”:“execute-api:Invoke”,“Effect”:“Allow” |拒绝“,”资源“:”some arn“}]},”context“:{”customInfo1“:”hello“,”customInfo2“:”world“}}

What I need:

  • 我需要将上面的上下文对象中传递的自定义信息传递给请求,因为它传递给目标函数 .

What I know:

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

2 回答

  • 2

    如果选中this document,您会发现可以创建自定义 Model 以从正文映射到 Headers ,反之亦然 . 然后,您可以在 Method Request - > Request Body 下分配此模型 .

  • 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 部分中,我通过自定义授权程序传递的所有键都已存在 .

相关问题