

package user.handler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.json.JSONObject;
public class Test0513Handler implements
RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request,
Context context) {
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
JSONObject requestObj = new JSONObject(request);
JSONObject requestContext = requestObj.getJSONObject("requestContext");
JSONObject authorizer = requestContext.getJSONObject("authorizer");
JSONObject claims = authorizer.getJSONObject("claims");
// username: github_125880884
String username = claims.getString("cognito:username");
String userId = username.replace("github_", "");
// 찐 userId !!!
context.getLogger().log("userId: " + userId);
return response;
}
}
간단하게 합치면
JSONObject requestObj = new JSONObject(request);
JSONObject requestContext = requestObj.getJSONObject("requestContext")
.getJSONObject("authorizer").getJSONObject("claims");
String username = requestContext.getString("cognito:username");
Integer userId = Integer.parseInt(username.replace("github_", ""));