简体   繁体   中英

User creation with keycloak

I am trying to create user with keycloak's /users endpoint in a spring boot project. These are the steps I have followed First created an admin in master realm and admin-cli client. Used that to get instance of keycloak for further operations.

return Keycloak.getInstance(
            keycloakProperties.getAuthServerUrl(),
            "master",
            adminConfig.getUserName(),
            adminConfig.getPassword(),
            "admin-cli");

I am able to get the user created if I don't add the client representation in user. If I add the CredentialRepresentation inside the userRepresentation object,I am getting the BadRequest error (400 code) without any details. I looked up the keycloak server logs that too wasnt helpful. As a try,I did the user creation first and reset password later,then also user created,but unable to set password with reset-password endpoint (same error).As mentioned in the documents,I have set the enabled property of userRepresentation to true. Keycloak version: 12.0.4,Spring boot version: 2.1.6.RELEASE I tried to read the entity from the response received, but failed in that too. Any help to figure out the issue will e appreciated. // Code for usercreation

UserRepresentation userRepresentation = new UserRepresentation();
    userRepresentation.setUsername(userModel.getEmail());
    userRepresentation.setEmail(userModel.getEmail());
    userRepresentation.setCredentials(Arrays.asList(createPasswordCredentials(userModel.getPassword())));
    userRepresentation.setEnabled(true);
    Keycloak keycloak = getKeycloakInstance(keycloakProperties);
    Response response = keycloak.realm(keycloakProperties.getRealm()).users().create(userRepresentation);
    log.info("Response Code {}", response.getStatus());

Code for CredentialRepresentation:

private static CredentialRepresentation  createPasswordCredentials(String password) {
    CredentialRepresentation passwordCredentials = new CredentialRepresentation();
    passwordCredentials.setTemporary(false);
    passwordCredentials.setType(CredentialRepresentation.PASSWORD);
    passwordCredentials.setValue(password);
    return passwordCredentials;
  }

I just want to update the real cause behind this issue,The server I used was of old version,and the issue got fixed when tested against latest keycloak server. Thank you for the support guys:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM