本文整理汇总了Java中org.keycloak.representations.idm.UserRepresentation.setCredentials方法的典型用法代码示例。如果您正苦于以下问题:Java UserRepresentation.setCredentials方法的具体用法?Java UserRepresentation.setCredentials怎么用?Java UserRepresentation.setCredentials使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.keycloak.representations.idm.UserRepresentation
的用法示例。
在下文中一共展示了UserRepresentation.setCredentials方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createUser
import org.keycloak.representations.idm.UserRepresentation; //导入方法依赖的package包/类
@Override
public String createUser(String username, String password, String firstname, String lastname, String email, List<String> rolenames) {
if (username.equals("user")) {
throw new UnsupportedOperationException("Dont't do that! (Sso rest api doesn't create user with username 'user' properly)");
}
CredentialRepresentation cr = new CredentialRepresentation();
cr.setType(CredentialRepresentation.PASSWORD);
cr.setValue(password);
cr.setTemporary(false);
UserRepresentation ur = new UserRepresentation();
ur.setUsername(username);
ur.setCredentials(Arrays.asList(cr));
ur.setFirstName(firstname);
ur.setLastName(lastname);
ur.setEmail(email);
ur.setEnabled(true);
Response response = client.realm(realmName).users().create(ur);
response.close();
String userId = getUserId(username);
client.realm(realmName).users().get(userId).resetPassword(cr);
if (rolenames != null && rolenames.size() > 0) {
addRealmRolesToUser(userId, rolenames);
}
return userId;
}
示例2: createUser
import org.keycloak.representations.idm.UserRepresentation; //导入方法依赖的package包/类
public void createUser(String realm, String userName, String password, long timeout, TimeUnit timeUnit) throws Exception {
int maxRetries = 10;
try (CloseableKeycloak keycloak = new CloseableKeycloak(endpoint, credentials, trustStore)) {
RealmResource realmResource = waitForRealm(keycloak.get(), realm, timeout, timeUnit);
for (int retries = 0; retries < maxRetries; retries++) {
try {
if (realmResource.users().search(userName).isEmpty()) {
UserRepresentation userRep = new UserRepresentation();
userRep.setUsername(userName);
CredentialRepresentation cred = new CredentialRepresentation();
cred.setType(CredentialRepresentation.PASSWORD);
cred.setValue(password);
cred.setTemporary(false);
userRep.setCredentials(Arrays.asList(cred));
userRep.setEnabled(true);
Response response = keycloak.get().realm(realm).users().create(userRep);
if (response.getStatus() != 201) {
throw new RuntimeException("Unable to create user: " + response.getStatus());
}
} else {
Logging.log.info("User " + userName + " already created, skipping");
}
break;
} catch (Exception e) {
Logging.log.info("Exception querying keycloak ({}), retrying", e.getMessage());
Thread.sleep(2000);
}
}
}
}
示例3: createUser
import org.keycloak.representations.idm.UserRepresentation; //导入方法依赖的package包/类
/**
* Creates a user in keycloak.
*
* @param userMrn MRN of the user
* @param firstName first name of user
* @param lastName last name of user
* @param password password of the user
* @param email email of the user
* @param orgMrn MRN of the org
* @throws IOException
*/
public void createUser(String userMrn, String password, String firstName, String lastName, String email, String orgMrn, String permissions, boolean enabled) throws IOException, DuplicatedKeycloakEntry {
log.debug("creating user: " + userMrn);
UserRepresentation user = new UserRepresentation();
user.setUsername(email);
user.setEnabled(enabled);
if (email != null && !email.trim().isEmpty()) {
user.setEmail(email);
user.setEmailVerified(true);
}
if (firstName != null && !firstName.trim().isEmpty()) {
user.setFirstName(firstName);
}
if (lastName != null && !lastName.trim().isEmpty()) {
user.setLastName(lastName);
}
// Set attributes
Map<String, List<String>> attr = new HashMap<>();
attr.put("org", Collections.singletonList(orgMrn));
attr.put("mrn", Collections.singletonList(userMrn));
if (permissions != null && !permissions.trim().isEmpty()) {
attr.put("permissions", Collections.singletonList(permissions));
}
user.setAttributes(attr);
Response ret = getProjectUserRealm().users().create(user);
String errMsg = ret.readEntity(String.class);
if (ret.getStatus() != 201) {
if (ret.getStatus() == 409) {
log.debug("creating user failed due to duplicated user" + errMsg);
throw new DuplicatedKeycloakEntry("User with mrn: " +userMrn + " already exists.", errMsg);
} else {
log.debug("creating user failed, status: " + ret.getStatus() + ", " + errMsg);
throw new IOException("User creating failed: " + errMsg);
}
}
log.debug("created user, status: " + ret.getStatus() + ", " + errMsg);
ret.close();
// Set credentials
CredentialRepresentation cred = new CredentialRepresentation();
cred.setType(CredentialRepresentation.PASSWORD);
cred.setValue(password);
// Make sure the user updates the password on first login
cred.setTemporary(true);
// Find the user by searching for the username
user = getProjectUserRealm().users().search(email, null, null, null, -1, -1).get(0);
user.setCredentials(Collections.singletonList(cred));
log.debug("setting password for user: " + user.getId());
getProjectUserRealm().users().get(user.getId()).resetPassword(cred);
log.debug("created user");
}
示例4: main
import org.keycloak.representations.idm.UserRepresentation; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
// new JHades().overlappingJarsReport();
Keycloak kc = KeycloakBuilder.builder() //
.serverUrl("https://localhost:8443/auth") //
.realm("master")//
.username("admin") //
.password("admin") //
.clientId("admin-cli") //
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()) //
.build();
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("test123");
credential.setTemporary(false);
UserRepresentation user = new UserRepresentation();
user.setUsername("testuser2");
user.setFirstName("Test2");
user.setLastName("User2");
user.setEmail("[email protected]");
user.setCredentials(Arrays.asList(credential));
user.setEnabled(true);
user.setRealmRoles(Arrays.asList("admin"));
// Create testuser
Response result = kc.realm("ait-platform").users().create(user);
if (result.getStatus() != 201) {
System.err.println("Couldn't create user.");
System.exit(0);
}
System.out.println("Testuser created.... verify in keycloak!");
System.out.println("Press any key...");
System.in.read();
// Delete testuser
String locationHeader = result.getHeaderString("Location");
String userId = locationHeader.replaceAll(".*/(.*)$", "$1");
UserResource user1 = kc.realm("ait-platform").users().get(userId);
System.out.println(user1.toRepresentation());
}