我在 Micronauts/Groovy 中编写了一个简单的 lambda 函数,以将允许/拒绝策略作为 AWS API 网关授权方返回。当用作 API 网关授权方时,无法解析 JSON
由于配置错误,执行失败:无法解析策略
在本地测试时,响应在 JSON 中具有正确的属性大小写。例如:
{
"principalId": "user",
"PolicyDocument": {
"Context": {
"stringKey": "1551172564541"
},
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow",
"Resource": "arn:aws:execute-api:eu-west-1:<account>:<ref>/*/GET/"
}
]
}}
当它在 AWS 中运行时,JSON 响应的属性全部为小写:
{
"principalId": "user",
"policyDocument": {
"context": {
"stringKey": "1551172664327"
},
"version": "2012-10-17",
"statement": [
{
"resource": "arn:aws:execute-api:eu-west-1:<account>:<ref>/*/GET/",
"action": "execute-api:Invoke",
"effect": "Allow"
}
]
}
}
不确定情况是否是问题,但我看不出还有什么问题(尝试了许多输出变化)。我尝试了各种 Jackson 注释(@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class) 等),但它们似乎对 AWS 的输出没有影响。
知道如何排序吗?谢谢。
示例代码:
试图让输出看起来像示例。
使用本地运行示例
runtime "io.micronaut:micronaut-function-web"
runtime "io.micronaut:micronaut-http-server-netty"
Lambda 函数处理程序:
AuthResponse sessionAuth(APIGatewayProxyRequestEvent event) {
AuthResponse authResponse = new AuthResponse()
authResponse.principalId = 'user'
authResponse.policyDocument = new PolicyDocument()
authResponse.policyDocument.version = "2012-10-17"
authResponse.policyDocument.setStatement([new session.auth.Statement(
Effect: Statement.Effect.Allow,
Action:"execute-api:Invoke",
Resource: "arn:aws:execute-api:eu-west-1:<account>:<ref>/*/GET/"
)])
return authResponse
}
AuthResponse 看起来像:
@CompileStatic
class AuthResponse {
String principalId
PolicyDocument policyDocument
}
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
@CompileStatic
class PolicyDocument {
String Version
List<Statement> Statement = []
}
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
@CompileStatic
class Statement {
String Action
String Effect
String Resource
}