在Lambda中,我从HttpRequest的Principal中获取名称。
protected String resolveUser( HttpRequest request ){
String ret = null;
Optional<Principal> principal = request.getUserPrincipal();
if( principal.isPresent() ){
ret = principal.get().getName();
}
if( ret == null || ret.length() == 0 ){
ret = "unknown";
}
return ret;
}
Cognito的identityId返回的字符串名称是什么?类似于这样:
us-east-1:xxxxe650-53f4-4cba-b553-5dff42bexxxx我想要记录实际用户登录或者至少有一种方法在需要时将identityId转换为登录信息。LookupDeveloperIdentityAPI似乎是正确的解决方法,但我无法让它正常工作。尝试使用Java和AWS Java SDK 2来实现此操作:
protected String loadUsername( String user ){
String ret = "unknown:"+user;
CognitoIdentityClient cognito = CognitoIdentityClient.create();
LookupDeveloperIdentityRequest request = LookupDeveloperIdentityRequest.builder()
.identityPoolId( identityPoolId )
.identityId( user )
.build();
LookupDeveloperIdentityResponse response = cognito.lookupDeveloperIdentity( request );
List<String> identifiers = response.developerUserIdentifierList();
if( identifiers != null && identifiers.size() > 0 ){
ret = identifiers.get( 0 );
}
return ret;
}
抛出异常
software.amazon.awssdk.services.cognitoidentity.model.NotAuthorizedException: 您没有访问此身份的权限(服务:CognitoIdentity,状态码:400,请求 ID:64e36646-612b-4985-91d1-82aca770XXXX)
通过CLI尝试执行此操作会产生类似的结果:
aws cognito-identity lookup-developer-identity --identity-id us-east-1:xxxxe650-53f4-4cba-b553-5dff42bexxxx --identity-pool-id us-east-1:xxxx0aa1-89f9-4418-be04-7e83c838xxxx --max-results=10
调用LookupDeveloperIdentity操作时发生错误(NotAuthorizedException):您无权访问此身份
我已确保IAM策略应该能够处理此操作,当我尝试使用没有此策略的角色进行尝试时,会出现不同的错误。
{
"Effect": "Allow",
"Action": [
"cognito-identity:LookupDeveloperIdentity"
],
"Resource": [
"arn:aws:cognito-identity:us-east-1:##########:identitypool/us-east-1:xxxx0aa1-89f9-4418-be04-7e83c838xxxx"
]
}
所以问题归结为:
- 这是从身份池ID获取用户池用户名的最佳方法吗?
- 如果是 - 我做错了什么?
- 如果不是 - 有更好的方法吗?
你确定你正在使用拥有所请求lookupDeveloperIdentity的身份池的账户的凭证吗?
- https://forums.aws.amazon.com/thread.jspa?threadID=231354 对我来说,这看起来像是用户权限问题,而不是IAM角色问题。 - Jan Garaj