【问题标题】:AWS Lambda response in Java for Cognito适用于 Cognito 的 Java 中的 AWS Lambda 响应
【发布时间】:2020-06-22 02:50:39
【问题描述】:

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

这样的东西被传递给 lambda 函数

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

现在我需要用 Java 做出响应.. 并发送回 Cognito。目前 Cognito 抛出 "InvalidLambdaResponseException"。

下面的 Java 代码只是返回事件..

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

【问题讨论】:

  • 您忽略了使用响应数据丰富事件。 Cognito 期望的数据因它发送的事件类型而异。
  • @TrentBartlem 它不期望任何响应数据。在 node.js 中你可以只返回事件。
  • 这取决于事件,请参阅:docs.aws.amazon.com/cognito/latest/developerguide/…。像CustomMessage_SignUp 这样的一些事件需要响应,但像PreAuthentication_Authentication 这样的其他事件则不需要。
  • @TrentBartlem 好吧,我的错,但我使用的事件没有。引发异常的是 lambda 函数。
  • AWS 文档的一部分 (docs.aws.amazon.com/lambda/latest/dg/…) 说您可以将 AWS 事件用作输入和输出;另一部分 (docs.aws.amazon.com/lambda/latest/dg/…) 没有。尝试输出一个字符串 (JSON) 并查看它是否有效,然后使用输入和输出类。

标签: java amazon-web-services aws-lambda aws-sdk amazon-cognito


【解决方案1】:

你只需要这样一个类:

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 模板的示例

【讨论】:

    【解决方案2】:

    如果你想用 Kotlin 编写你的 lambda。是这样的

    拉姆达:

    class ClientSignupLambda : RequestHandler<PostConfirmationConfirmSignUp, PostConfirmationConfirmSignUp> {
    
        override fun handleRequest(event: PostConfirmationConfirmSignUp, context: Context): PostConfirmationConfirmSignUp {
            context.logger.log("event toString $event")
            return event
        }
    
    }
    

    Json 类:

    /**
     * Example of the JSON this class represents:
     * // formatterOFF
     * {
     *  version=1,
     *  region=eu-west-2,
     *  userPoolId=eu-west-2_abcd123,
     *  "userName=foo@gmail.com",
     *  "callerContext="{
     *      "awsSdkVersion=aws-sdk-unknown-unknown",
     *      clientId=123456789123456789
     *  },
     *  "triggerSource=PostConfirmation_ConfirmSignUp",
     *  "request="{
     *      "userAttributes="{
     *          sub=12341234-1234-1234-1234-123412341234,
     *          "cognito":"user_status=CONFIRMED",
     *          "email_verified=true",
     *          "name=Blundell",
     *          "email=foo@gmail.com"
     *      }
     *  },
     *  "response="{}
     * }
     * // formmatterON
     */
    @Suppress("PropertyName") // Names match example, as they are used to parse JSON
    class PostConfirmationConfirmSignUp {
        var version = 0
        var region: String? = null
        var userPoolId: String? = null
        var userName: String? = null
        var callerContext: CallerContext? = null
        var triggerSource: String? = null
        var request: Request? = null
        var response: Response? = null
    
        class CallerContext {
            var awsSdkVersion: String? = null
            var clientId: String? = null
            override fun toString(): String {
                return "CallerContext(awsSdkVersion=$awsSdkVersion, clientId=$clientId)"
            }
        }
    
        class Request {
            var userAttributes: UserAttributes? = null
            override fun toString(): String {
                return "Request(userAttributes=$userAttributes)"
            }
        }
    
        class UserAttributes {
            var sub: String? = null
            var cognito: Cognito? = null
            var email_verified: String? = null
            var name: String? = null
            var email: String? = null
            override fun toString(): String {
                return "UserAttributes(sub=$sub, cognito=$cognito, email_verified=$email_verified, name=$name, email=$email)"
            }
        }
    
        class Cognito {
            var user_status: String? = null
            override fun toString(): String {
                return "Cognito(user_status=$user_status)"
            }
        }
    
        class Response {
            override fun toString(): String {
                return "Response()"
            }
        }
    
        override fun toString(): String {
            return "Example(version=$version, region=$region, userPoolId=$userPoolId, userName=$userName, callContext=$callerContext, triggerSource=$triggerSource, request=$request, response=$response)"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 2016-08-31
      • 2018-03-11
      • 2017-06-03
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多