我已将新用户和角色插入到keycloak中。但是如何连接用户和角色实体,如user-role
Keycloak kc = KeycloakBuilder.builder()
.serverUrl("http://localhost:8080/auth")
.realm("master")
.username("admin")
.password("admin")
.clientId("admin-cli")
.resteasyClient(
new ResteasyClientBuilder()
.connectionPoolSize(10).build()
).build();
//creating role
RoleRepresentation roleRepresentation = new RoleRepresentation();
roleRepresentation.setName("latest_role");
kc.realm("master").roles().create(roleRepresentation);
//creating user
UserRepresentation user = new UserRepresentation();
user.setUsername("anoop");
user.setFirstName("anoop");
user.setLastName("anoop");
user.setEnabled(true);
user.setCredentials(Arrays.asList(credential));
kc.realm("master").users().create(user);
答案 0 :(得分:3)
这似乎是一个不小的问题。通常我希望user.setRealmRoles(roleRepresentation)
中的此功能与this example中一样。但那不适合我。
相反,我使用此功能将roleRepresentation
添加到user
。
RoleRepresentation savedRoleRepresentation = kc.realm("master").roles()
.get("latest_role").toRepresentation();
kc.realm("master").users().get(userId).roles().realmLevel()
.add(asList(savedRoleRepresentation));
完整代码:
Keycloak kc = Keycloak.getInstance("http://localhost:8080/auth",
"master", "admin", "admin", "admin-cli");
RoleRepresentation roleRepresentation = new RoleRepresentation();
roleRepresentation.setName("latest_role");
kc.realm("Test").roles().create(roleRepresentation);
UserRepresentation user = new UserRepresentation();
user.setUsername("test");
user.setFirstName("test");
user.setLastName("test");
user.setEnabled(true);
user.setCredentials(Arrays.asList(credential));
Response response = kc.realm("Test").users().create(user);
String userId = getCreatedId(response);
System.out.println("Testuser created.... verify in keycloak!");
RoleRepresentation savedRoleRepresentation = kc.realm("Test").roles()
.get("latest_role").toRepresentation();
kc.realm("Test").users().get(userId).roles().realmLevel()
.add(asList(savedRoleRepresentation));
private String getCreatedId(Response response) {
URI location = response.getLocation();
if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
Response.StatusType statusInfo = response.getStatusInfo();
throw new WebApplicationException("Create method returned status " +
statusInfo.getReasonPhrase() + " (Code: " + statusInfo.getStatusCode() + "); expected status: Created (201)", response);
}
if (location == null) {
return null;
}
String path = location.getPath();
return path.substring(path.lastIndexOf('/') + 1);
}
修改强>
似乎没有为用户保存凭据。要强制keycloak保存它们,您必须使用以下命令重置密码:
UserResource userResource = kc.realm("Test").users().get(userId);
userResource.resetPassword(credential);
答案 1 :(得分:0)
UserRepresentation user = new UserRepresentation();
user.setEmail("naveenm@gmail.com");
user.setUsername("naveenm555");
user.setFirstName("naveen");
user.setLastName("m");
user.setEnabled(true);
Response response = kc.realm("appscook").users().create(user);
System.out.println("user created.... verify in keycloak!");
String userId = getCreatedId(response);
UserResource userResource=kc.realm("appscook").users().get(userId);
CredentialRepresentation newCredential = new CredentialRepresentation();
newCredential.setType(CredentialRepresentation.PASSWORD);
newCredential.setValue("naveenm555");
newCredential.setTemporary(false);
userResource.resetPassword(newCredential);
System.out.println("role created.");
此代码将为用户创建新用户和设置角色。它将允许用户登录。上述代码不允许用户在创建后登录。