适用于Cognito的Java中的AWS Lambda响应 [英] AWS Lambda response in Java for Cognito

查看:187
本文介绍了适用于Cognito的Java中的AWS Lambda响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用Java编写 AWS Lambda响应以使Cognito满意?

How can I write an "AWS Lambda response" in Java so that Cognito is happy?

类似的东西传递给lambda函数

Something like this is passed to the lambda function

{
"version": number,
"triggerSource": "string",
"region": AWSRegion,
"userPoolId": "string",
"callerContext": 
    {
        "awsSdkVersion": "string",
        "clientId": "string"
    },
"request":
    {
        "userAttributes": {
            "string": "string",
            ....
        }
    },
"response": {}
}

现在,我需要使用Java进行响应。然后发送回Cognito。此刻Cognito引发 InvalidLambdaResponseException。

Now I need to make the response in Java.. and send back to Cognito. At the moment Cognito throws an "InvalidLambdaResponseException".

下面的Java代码仅返回事件。

Java code below just returns the event..

public class LambdaFunctionHandler implements RequestHandler<CognitoEvent, CognitoEvent> 
{
    @Override
    public CognitoEvent handleRequest(CognitoEvent arg0, Context arg1) 
    {
        return arg0;
    }
}


推荐答案

您只是需要一个像这样的类:

You just need a class like this:

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JsonSerialize
public class Example {
    private int version;
    private String triggerSource;
    private String region;
    private String userPoolId;
    private Map<String, String> callerContext;
    private Request request;
    private Response response;

    @Getter
    @Setter
    @JsonSerialize
    public static class Request {
        private Map<String, String> userAttributes;
        public Request(Map<String, String> userAttr) {
            userAttributes = userAttr;
        }
    }

    @Getter
    @Setter
    @JsonSerialize
    public static class Response { }

}

序列化后的样子如下:

{
  "version" : 1,
  "triggerSource" : "trigger",
  "region" : "us-east-1",
  "userPoolId" : "user-pool-id",
  "callerContext" : {
    "some-key" : "some-value"
  },
  "request" : {
    "userAttributes" : {
      "name" : "Michael J Leonard"
    }
  },
  "response" : { }
}

并将其作为lambda的输入。可能需要进行一些更改,但这是PostAuthentication lambda模板的示例

And have this as an input to your lambda. It might require some changes but this is an example of a template for the PostAuthentication lambda

这篇关于适用于Cognito的Java中的AWS Lambda响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆