在无服务器Java函数中获取AWS Cognito用户ID
我正在学习教程,它使用无服务器框架创建一个API,将对象插入DynamoDB表,并将它们与经过身份验证的AWS Cognito用户关联。我试图将Node.js代码转换为Java,但在获取Cognito标识时遇到了一个问题,如图所示 我希望以下几行Java代码是等效的:在无服务器Java函数中获取AWS Cognito用户ID,java,aws-lambda,serverless-framework,serverless,Java,Aws Lambda,Serverless Framework,Serverless,我正在学习教程,它使用无服务器框架创建一个API,将对象插入DynamoDB表,并将它们与经过身份验证的AWS Cognito用户关联。我试图将Node.js代码转换为Java,但在获取Cognito标识时遇到了一个问题,如图所示 我希望以下几行Java代码是等效的: final CognitoIdentity identity = context.getIdentity(); final String userId = identity.getIdentityId(); 但是userId为空
final CognitoIdentity identity = context.getIdentity();
final String userId = identity.getIdentityId();
但是userId
为空
我正在使用aws api gateway cli test实用程序调用带有Cognito用户凭据的api,如图所示。身份验证通过,但处理程序中的userId
为空
这是我的职责:
package com.mealplanner.function;
import java.util.Map;
import com.amazonaws.services.lambda.runtime.CognitoIdentity;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mealplanner.dal.MealRepository;
import com.mealplanner.domain.Meal;
import com.serverless.ApiGatewayResponse;
public class CreateMealHandler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {
@Override
public ApiGatewayResponse handleRequest(final Map<String, Object> request, final Context context) {
try {
final CognitoIdentity identity = context.getIdentity();
final String userId = identity.getIdentityId();
final JsonNode body = new ObjectMapper().readTree((String) request.get("body"));
final MealRepository repository = new MealRepository();
final Meal meal = new Meal();
meal.setUserId(userId);
meal.setDescription(body.get("description").asText());
repository.save(meal);
return ApiGatewayResponse.builder()
.setStatusCode(200)
.setObjectBody(meal)
.build();
} catch (final Exception e) {
final String errorText = String.format("Error saving meal with request [%s]", request);
LOGGER.error(errorText, e);
return ApiGatewayResponse.builder()
.setStatusCode(500)
.setObjectBody(errorText)
.build();
}
}
}
我是否缺少一些配置,或者没有正确翻译Node.js代码
如果我遗漏了任何相关信息,请在此处提供完整代码:。我将使用缺少的任何内容更新此问题,以确保所有相关信息都是独立的。结果表明我没有正确翻译Node.js代码。要访问
CognitoIdentityId
我必须从request
对象获取requestContext
,然后获取identity
对象,如下所示:
public ApiGatewayResponse handleRequest(final Map<String, Object> request, final Context context) {
final Map<String, Object> requestContext = (Map<String, Object>) request.get("requestContext");
final Map<String, Object> identity = (Map<String, Object>) requestContext.get("identity");
final String userId = (String) identity.get("cognitoIdentityId");
// etc
}
public-ApiGatewayResponse-handleRequest(最终映射请求、最终上下文){
final-Map-requestContext=(Map)request.get(“requestContext”);
最终映射标识=(Map)requestContext.get(“标识”);
最终字符串userId=(String)identity.get(“cognitoIdentityId”);
//等
}
createMeal:
handler: com.mealplanner.function.CreateMealHandler
events:
- http:
path: /meals
method: post
cors: true
authorizer: aws_iam
public ApiGatewayResponse handleRequest(final Map<String, Object> request, final Context context) {
final Map<String, Object> requestContext = (Map<String, Object>) request.get("requestContext");
final Map<String, Object> identity = (Map<String, Object>) requestContext.get("identity");
final String userId = (String) identity.get("cognitoIdentityId");
// etc
}