Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to log in in Keycloak after create user with Admin Rest API with Spring Boot

After successfully creating a user by Admin Rest API in Keycloak (2.4.0.Final) I can not log in using this user. I can only enter the administration console and reset the user's password.

Follow the code used.

    Keycloak keycloak = Keycloak.getInstance(
            "http://localhost:8080/auth",
            "master",
            "admin",
            "admin",
            "admin-cli");

    CredentialRepresentation credential = new CredentialRepresentation();
    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue("12345678");
    credential.setTemporary(false);

    UserRepresentation user = new UserRepresentation();
    user.setUsername("testBackend");
    user.setFirstName("testBackend");
    user.setLastName("testBackend");
    user.setEmail("[email protected]");
    user.setEnabled(true);
    user.setCredentials(Arrays.asList(credential));

    keycloak.realm("realm-test").users().create(user);
like image 928
Danilo Andrade Avatar asked Dec 06 '25 19:12

Danilo Andrade


1 Answers

keycloak does not support the create user(API) with password, u need to make another call to register the password for this user:

UserResource userResource=kc.realm("master").users().get(id);
CredentialRepresentation newCredential = new CredentialRepresentation();
newCredential.setType(PASSWORD);
newCredential.setValue("Tersting1");
newCredential.setTemporary(false);
userResource.resetPassword(newCredential);`
like image 89
Naveen phougat Avatar answered Dec 09 '25 08:12

Naveen phougat