Keycloak 创建新用户失败

Keycloak create new user fails

尝试使用以下代码创建新用户:

Keycloak kc = Keycloak.getInstance(
"http://192.168.11.55:8080/auth", 
"master", // the realm to log in to
"admin", "pass",  // the user
"security-admin-console");

CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("test123");
UserRepresentation user = new UserRepresentation();
user.setUsername("testuser");
user.setFirstName("Test");
user.setLastName("User");
user.setCredentials(Arrays.asList(credential));
kc.realm("master").users().create(user);

它 returns HTTP 400 错误请求。 Keycloak 日志说:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "origin" (class     org.keycloak.representations.idm.UserRepresentation), not marked as ignorable (22 known properties: "federatedIdentities", "enabled",     "lastName", "emailVerified", "clientConsents", "self", "socialLinks", "applicationRoles", "createdTimestamp", "groups", "username",     "attributes", "id", "firstName", "email", "federationLink", "serviceAccountClientId", "requiredActions", "realmRoles", "clientRoles",     "totp", "credentials"])
 at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@250fdbe0; line: 1, column: 37] (through reference chain:     org.keycloak.representations.idm.UserRepresentation["origin"])    

我正在使用 Keycloak 2.3.0.Final 和 Keycloak Admin REST Client 2.4.0.Final API.

您的 KeyCloak 服务器和 Keycloak Admin REST 客户端应该是相同的版本。一些字段可能是在 2.4.0.Final 版本中新添加的。

同样值得一提的是,在某些旧版本的 Keycloak 中,不可能在同一个 kc.realm("master").users().create(user); 调用中既创建新用户又设置凭据。

这可以通过首先创建用户而不是调用 setCredentials,然后将它们设置为另一个调用来解决。