gpt4 book ai didi

java - AWS API Gateway 自定义授权方 lambda

转载 作者:行者123 更新时间:2023-11-29 07:29:34 26 4
gpt4 key购买 nike

我正在尝试通过 AWS API Gateway 的自定义授权方授权 API 调用,
这基本上是一个自定义的 lambda 函数,它接受以下格式的 header -

{
"authorizationToken": "0c34ba00bde34200b383abe22bcfef96",
"methodArn": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"type": "TOKEN"
}

并期待以下格式的响应 -

{
"principalId": "xxxxxxx", // the principal user identification associated with the token send by the client
"policyDocument": { // example policy shown below, but this value is any valid policy
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"execute-api:Invoke"
],
"Resource": [
"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:xxxxxxxx:/test/*/mydemoresource/*"
]
}
]
}
}

我能够使用 authorizationToken 执行内部逻辑,并验证函数是否应以“允许”或“拒绝”策略响应
但是当我尝试从控制台测试授权器时出现解析错误,
以下是我的请求日志-

Execution log for request test-request
Thu Jun 29 11:48:10 UTC 2017 : Starting authorizer: 1o3dvk for request: test-request
Thu Jun 29 11:48:10 UTC 2017 : Incoming identity: **************************cfef96
Thu Jun 29 11:48:10 UTC 2017 : Endpoint request URI: https://lambda.ap-southeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-southeast-1:855399270504:function:um_guestSessionAuthoriser/invocations
Thu Jun 29 11:48:10 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=*********************************************************************************************************************************************************************************************************************************************************************************************************************************************751e60, X-Amz-Date=20170629T114810Z, x-amzn-apigateway-api-id=z6t3cv0z4m, X-Amz-Source-Arn=arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/authorizers/1o3dvk, Accept=application/json, User-Agent=AmazonAPIGateway_z6t3cv0z4m, X-Amz-Security-Token=FQoDYXdzEHQaDOcIbaPscYGsl1wF4iLBAxzOTpZlR2r3AO3g96xwhRuQjEhU9OjOaRieBWQPeosNqv53aGKnBTT2CmkrVzHo3UqOdT1eakuS7tAXAbEcUIHVheWpBnvxqTkaPcknRL7QE79RSqVeryoXo2R1Kmk0Q9Iq+JGFlOJYQQJqvY/hcUg189xqbpTGrhZjcA+pjuSp+M9D97Kce0VP0e3peu/YvON0eGvUlj59MAJAwGVPIzplMKTDFrFg5NKEj79RSxNrNE8y4bAebOwlD8xLv649Zny7++xlMBBwHqMNHu3K9lFXSnKY9DHf6kvezZmpoFB2uu8WbrpInH0eQ/bIAd [TRUNCATED]
Thu Jun 29 11:48:10 UTC 2017 : Endpoint request body after transformations: {"type":"TOKEN","methodArn":"arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/","authorizationToken":"0c34ba00bde34200b383abe22bcfef96"}
Thu Jun 29 11:48:10 UTC 2017 : Sending request to https://lambda.ap-southeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-southeast-1:855399270504:function:um_guestSessionAuthoriser/invocations
Thu Jun 29 11:48:21 UTC 2017 : Authorizer result body before parsing: {"principalId":"user","policyDocument":{"version":"2012-10-17","statement":[{"resource":"arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/","action":"execute-api:Invoke","effect":"Allow"}]}}
Thu Jun 29 11:48:21 UTC 2017 : Execution failed due to configuration error: Could not parse policy: {"version":"2012-10-17","statement":[{"resource":"arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/","action":"execute-api:Invoke","effect":"Allow"}]}
Thu Jun 29 11:48:21 UTC 2017 : AuthorizerConfigurationException

我在 Lambda 函数上使用 Java,我已经使用 PoJo 类(setter-getter 类)构建并返回了策略
美化 lambda 响应后,我的策略如下所示 -

{
"principalId": "user",
"policyDocument": {
"version": "2012-10-17",
"statement": [{
"resource": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"action": "execute-api:Invoke",
"effect": "Allow"
}]
}
}

我想知道为什么它无法解析我的回复?
根据建议,我尝试利用响应 IAM 政策,
我使用 com.google.gson.annotations.SerializedName 导入的 @SerializedName 得到了以下输出 -

{
"principalId": "user",
"policyDocument": {
"version": "2012-10-17",
"statement": [{
"effect": "Deny",
"action": ["execute-api:Invoke"],
"resource": ["arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/"]
}]
}
}

但看起来我的 lambda 响应和 API 网关之间发生了一些奇怪的事情,
变量在某处内部小写,
我仍然遇到同样的解析错误,
它可以接受其他格式的响应吗?字符串也不起作用。

我还应该尝试什么?我的保单格式有误吗?
我从这些网站获得了两种不同的政策格式 -
1. http://docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html
2. https://aws.amazon.com/blogs/compute/introducing-custom-authorizers-in-amazon-api-gateway/

最佳答案

您的政策属性需要适当的大写。而不是:

{
"principalId": "user",
"policyDocument": {
"version": "2012-10-17",
"statement": [{
"resource": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"action": "execute-api:Invoke",
"effect": "Allow"
}]
}
}

应该是:

{
"principalId": "user",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Resource": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"Action": "execute-api:Invoke",
"Effect": "Allow"
}]
}
}

不妨使用“PrincipalId”来保持一致。

关于java - AWS API Gateway 自定义授权方 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44825811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com