[keycloak-user] Create user with roles using java client

Marko Strukelj mstrukel at redhat.com
Wed Dec 21 09:45:48 EST 2016


You need to set user's password as a separate step from user creation.

First create a user:

UserRepresentation user = new UserRepresentation();
user.setUsername(email.getAddress());
user.setRealmRoles(Arrays.asList(someRole));

Response response = kc.realm(appRealm).users().create(user);
String userId = ApiUtil.getCreatedId(response);
response.close();

Then set a password:

CredentialRepresentation cred = new CredentialRepresentation();
cred.setType(CredentialRepresentation.PASSWORD);
cred.setValue("password");
cred.setTemporary(false);

realm.users().get(userId).resetPassword(cred);

On Wed, Dec 21, 2016 at 4:26 AM, Steve Chernyak <mrrothstein at gmail.com>
wrote:

> Forgot to mention, I did set the enabled attribute.
>
> Current version:
>
>         CredentialRepresentation credential = new
> CredentialRepresentation();
>         credential.setType(CredentialRepresentation.PASSWORD);
>         credential.setValue(password);
>         UserRepresentation user = new UserRepresentation();
>         user.setUsername(email.getAddress());
>         user.setEmail(email.getAddress());
>         user.setCredentials(Arrays.asList(credential));
>         user.setRealmRoles(Arrays.asList(Role.OWNER.authority()));
>         user.setEnabled(true);
>         Response response = kc.realm(appRealm).users().create(user);
>
> On Tue, Dec 20, 2016 at 10:20 PM, Steve Chernyak <mrrothstein at gmail.com>
> wrote:
>
> > I would really appreciate some help with this...
> >
> > Am I missing something in the documentation? I can't find anything that
> > describes this process in detail.
> >
> > It also doesn't appear that the password I'm using during the creation
> > process is honored either. I have to change the password through the
> > console to get the login to work.
> >
> > Is there a specific set of attributes that are used for the user creation
> > process while others are ignored? Is there anything that documents the
> > correct way to do what i'm trying?
> >
> > Thanks
> >
> > On Sat, Dec 17, 2016 at 11:59 PM, Steve Chernyak <mrrothstein at gmail.com>
> > wrote:
> >
> >> Hello,
> >>
> >> I'm trying to create a user associated with a role:
> >>
> >> CredentialRepresentation credential = new CredentialRepresentation();
> >> credential.setType(CredentialRepresentation.PASSWORD);
> >> credential.setValue(password);
> >> UserRepresentation user = new UserRepresentation();
> >> user.setUsername(email.getAddress());
> >> user.setCredentials(Arrays.asList(credential));
> >> user.setRealmRoles(Arrays.asList(someRole));
> >> Response response = kc.realm(appRealm).users().create(user);
> >>
> >> The response status is the expected 201 and I can see the user in the
> >> realm through the admin console. However, the user is not associated
> with
> >> "someRole"...
> >>
> >> I'm not sure what I'm missing...
> >>
> >> How should I go about creating a user associated with a role
> >> progrmatically?
> >>
> >> Thanks
> >>
> >
> >
> _______________________________________________
> keycloak-user mailing list
> keycloak-user at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user
>


More information about the keycloak-user mailing list