【问题标题】:Get AWS Cognito user ID in Serverless Java function在无服务器 Java 函数中获取 AWS Cognito 用户 ID
【发布时间】:2018-12-28 04:55:22
【问题描述】:

我正在关注https://serverless-stack.com/ 教程,该教程使用无服务器框架创建一个 API,该 API 将对象插入 DynamoDB 表并将它们与经过身份验证的 AWS Cognito 用户相关联。我正在尝试将 Node.js 代码转换为 Java,但在获取 Cognito 身份时遇到了问题,如图所示 on this page

userId: event.requestContext.identity.cognitoIdentityId,

我希望以下 Java 代码行是等效的:

final CognitoIdentity identity = context.getIdentity();
final String userId = identity.getIdentityId();

但是userId 是空的。

我正在使用 aws-api-gateway-cli-test 实用程序使用 Cognito 用户的凭据调用我的 API,如 on this page 所示。身份验证通过,但处理程序中的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();
        }
    }
}

这是 serverless.yml 中的函数定义:

createMeal:
    handler: com.mealplanner.function.CreateMealHandler
    events:
      - http:
          path: /meals
          method: post
          cors: true
          authorizer: aws_iam

是我遗漏了一些配置还是我没有正确翻译 Node.js 代码?

如果我错过了任何相关信息,请在此处获取完整代码:https://github.com/stuartleylandcole/meal-planner/tree/add-users。我会用任何缺失的内容更新这个问题,以确保所有相关信息都是独立的。

【问题讨论】:

    标签: java aws-lambda serverless-framework serverless


    【解决方案1】:

    原来我没有正确翻译 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
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 2016-10-22
      • 2020-08-31
      • 2020-02-16
      • 2018-01-13
      相关资源
      最近更新 更多