是否可以在API网关上添加来自AWS Custom Auth的HTTP标头?

Cra*_*son 8 amazon-web-services aws-lambda aws-api-gateway

我在AWS API Gateway上使用Custom Auth,但我想根据结果添加额外的HTTP头.有谁知道这是否可能,或者如何做到这一点.如果不是,是否有可能会出现这种情况?

非常感谢.

Jac*_*AWS 10

我们最近加入了对此的支持.文档应该很快就会出现.

现在您可以从授权器函数返回这样的对象:

{
  "principalId": "xxxxxxxx", // The principal user identification associated with the token send by the client.
  "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "execute-api:Invoke",
        "Effect": "Allow|Deny",
        "Resource": "arn:aws:execute-api:<regionId>:<accountId>:<appId>/<stage>/<httpVerb>/[<resource>/<httpVerb>/[...]]"
      }
    ]
  },
  "context" : {
    "key" : "value",
    "numKey" : 1,
    "boolKey" : true
  }
}
Run Code Online (Sandbox Code Playgroud)

不允许使用数组和对象,只将字符串/数字/布尔值作为有效的JSON.必须命名根密钥context.

您可以在请求$上下文中访问这些值,如下所示:

$context.authorizer.key -> value 
$context.authorizer.numKey -> 1
$context.authorizer.boolKey -> true
Run Code Online (Sandbox Code Playgroud)

所以要回答你的问题,你不能有条件地添加标题,但你可以设置标题值$context.authorizer.yourKey,如果yourKey没有在授权者响应中设置,标题值将为空(但标题仍然会被发送).

编辑:

文档是实时的http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html#api-gateway-custom-authorizer-output


Ema*_*nha 5

经过一天的努力,我终于成功了,希望我能把别人从这样的困境中拯救出来。这为杰克斯的回应增添了更多内容。基本上,您可以使用正文映射模板动态添加标头

  1. 创建授权者 Lambda(您可以使用授权者蓝图 lambda 开始),执行业务逻辑来创建 AuthPolicy 并使用键/值填充上下文对象。
  2. 在 API 网关上,选择资源,单击方法请求并将身份验证设置为您的授权者 lambda
  3. 打开Method Execution,选择集成类型并确保取消选择使用 Lambda 代理集成(如果您的请求指向 lambda)
  4. 添加身体映射模板- 从模板创建一个,您可以在此处访问 $context.authorizer.key
  5. 将以下内容添加到您的模板中(就在“body-json”下:$input.json('$'),”即可)
"headers": {
     "key-header" : "$util.escapeJavaScript($context.authorizer.key)",
     #foreach($param in $input.params().header.keySet())
         "$param": "$util.escapeJavaScript($input.params().header.get($param))" 
         #if($foreach.hasNext),#end
     #end },
Run Code Online (Sandbox Code Playgroud)

这将添加一个名为“key-header”的新标头,并转发所有原始标头,允许您将 user_id、user_role 等附加信息附加到上游服务。