我在AWS API Gateway上使用Custom Auth,但我想根据结果添加额外的HTTP标头。有谁知道这是否可能,或者如何做到这一点。如果不是,是否有可能会出现这种情况?
非常感谢。
答案 0 :(得分:9)
我们最近加入了对此的支持。文档应该很快就会出现。
现在您可以从授权程序函数返回这样的对象:
{
"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
}
}
不允许使用数组和对象,只将字符串/数字/布尔值作为有效的JSON。根密钥必须命名为context
。
您可以在请求$上下文中访问这些值,如下所示:
$context.authorizer.key -> value
$context.authorizer.numKey -> 1
$context.authorizer.boolKey -> true
因此,要回答您的问题,您无法有条件地 添加 标题,但您可以将标头值设置为$context.authorizer.yourKey
,如果{ {1}}未在授权者响应中设置,标头值将为空(但仍会发送标头)。
修改:
答案 1 :(得分:1)
在拔掉头发一天之后,我能够让这个工作得好,希望我可以救一个人。它为Jacks&#39;添加了更多。响应。 基本上,您可以使用正文映射模板
动态添加标题"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 },
这将添加一个名为&#34; key-header&#34;的新标题。并转发所有原始标题,允许您向上游服务附加其他信息,如user_id,user_role等。
答案 2 :(得分:0)
您只能从授权人结果获取PrincipalId,在您的集成请求中,您可以使用private LinearLayout layout1;
private LinearLayout layout2;
private LinearLayout layout3;
答案 3 :(得分:0)
我在上面尝试了伊曼纽尔·坎哈(Emanuel Canha)的答案,但似乎不再起作用。我昨天(2019年6月5日)工作的方式是
key-header
)context.authorizer.yourKey
作为映射自条目。 (请注意,您do not use the $ in this field。)