AWS Cognito 可以做到这一点吗?我还想在 DynamoDB 中添加用户和实体之间的“关系”。
有人遇到过这种情况 - 还是我使用了 AWS 的错误服务?
AWS Cognito 可以做到这一点吗?我还想在 DynamoDB 中添加用户和实体之间的“关系”。
有人遇到过这种情况 - 还是我使用了 AWS 的错误服务?
如果有人需要 Java SDK 的实际代码,这里有一个在后端进行身份验证的示例:
Map<String, String> params = new HashMap<>();
params.put("USERNAME", userId);
params.put("SECRET_HASH", calculateSecretHash(userId));
params.put("PASSWORD", rawPassword);
AdminInitiateAuthRequest request = new AdminInitiateAuthRequest()
.withUserPoolId("YOUR_USER_POOL_ID")
.withClientId("YOUR_USER_POOL_APP_CLIENT_ID")
.withAuthFlow(AuthFlowType.ADMIN_NO_SRP_AUTH)
.withAuthParameters(params);
AWSCognitoIdentityProvider identityProvider = AWSCognitoIdentityProviderClientBuilder.standard()
.withCredentials(credentialsProvider)
.withRegion(Regions.US_WEST_2)
.build();
AdminInitiateAuthResult result = identityProvider.adminInitiateAuth(request);
辅助功能:
private String calculateSecretHash(@Nonnull String userName) {
SecretKeySpec signingKey = new SecretKeySpec(m_clientSecret.getBytes(StandardCharsets.UTF_8), HmacAlgorithms.HMAC_SHA_256.toString());
try {
Mac mac = Mac.getInstance(HmacAlgorithms.HMAC_SHA_256.toString());
mac.init(signingKey);
mac.update(userName.getBytes(StandardCharsets.UTF_8));
byte[] rawHmac = mac.doFinal(m_clientId.getBytes(StandardCharsets.UTF_8));
return Base64.encodeBase64String(rawHmac);
} catch (Exception ex) {
throw new PgkbRuntimeException("Error calculating secret hash", ex);
}
}
对于发现此问题的其他任何人,现在可以使用 Cognito 用户池。更多信息可在此处获得。