[英]keycloak: check if old password matches, if so change to another one
Java with maven dependency "keycloak-admin-cli"具有 maven 依赖项“keycloak-admin-cli”的 Java
I have this code:我有这个代码:
keycloakService.findUserByEmailOrUsername( user.getKeycloakUsername() )
.ifPresent( userRepresentation -> {
//Check for old password
if ( userRepresentation.getCredentials() != null ) {
for (CredentialRepresentation c : userRepresentation.getCredentials()) {
if ( CredentialRepresentation.PASSWORD.equals( c.getType() ) ) {
if ( userDTO.getOldpassword().equals( c.getValue() ) ) {
//Das alte Passwort stimmt mit dem in der Datenbank überein. Wir können updaten
//Neues Passwort setzen
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType( CredentialRepresentation.PASSWORD );
credential.setValue( userDTO.getPassword() );
credential.setTemporary( false );
userRepresentation.setCredentials( Collections.singletonList( credential ) );
} else {
throw new RuntimeException( "Your current password does not match", null );
}
}
}
}
} );
I checked with the debugger and I get the correct user.我检查了调试器,得到了正确的用户。 "userRepresentation" is not null. “userRepresentation”不为空。 But the credentials of the user are always null.但是用户的凭据始终为空。
Also if I only want to set a new password for the user, it does not update:此外,如果我只想为用户设置新密码,它不会更新:
keycloakService.findUserByEmailOrUsername( user.getKeycloakUsername() )
.ifPresent( userRepresentation -> {
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType( CredentialRepresentation.PASSWORD );
credential.setValue( userDTO.getPassword() );
credential.setTemporary( false );
userRepresentation.setCredentials( Collections.singletonList( credential ) );
} );
I don't get an error message, keycloak just doesn't update.我没有收到错误消息,keycloak 只是没有更新。
Can anyone show me an example how I can check the old password and change it to another one?任何人都可以向我展示如何检查旧密码并将其更改为另一个密码的示例吗? thx谢谢
For the updating the password use :对于更新密码使用:
userRessource.get(userId).resetPassword(credential);
see this example : https://gist.github.com/thomasdarimont/0c136d0b8d339b997928e9bef225f941看这个例子: https : //gist.github.com/thomasdarimont/0c136d0b8d339b997928e9bef225f941
But for checking the actual credential, I didn't manage to check if there's one, as you say userRepresentation.getCredentials() is always null, even after reseting a new password.但是为了检查实际凭证,我没有设法检查是否有凭证,正如您所说的 userRepresentation.getCredentials() 始终为空,即使在重置新密码之后也是如此。
Try using the UserResource instead of the UserRepresentation.尝试使用 UserResource 而不是 UserRepresentation。
userResource.get(userId).credentials()
instead of userRepresentation.getCredentials()
should work, but it does feel more like a workaround. userResource.get(userId).credentials()
而不是userRepresentation.getCredentials()
应该可以工作,但它确实更像是一种解决方法。
As of 7/26/2021 getCredentials keeps always returning null.截至 2021 年 7 月 26 日,getCredentials 始终返回 null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.