我使用的示例Java代码:
public static AjaxResponse createUser(User newUser) {
Keycloak keycloak = Keycloak.getInstance(
SERVER_URL,
REALM,
USERNAME,
PASSWORD,
CLIENT_ID);
// Get Realm
RealmResource realmResource = keycloak.realm(REALM);
UsersResource userResource = realmResource.users();
// Create User Representation
UserRepresentation user = getUserRepresentation(newUser);
// Create user (requires manage-users role)
try {
System.out.println("Username: {}", userResource.get("USER-ID-HERE").toRepresentation().getUsername());
System.out.println("Count: " + userResource.count());
Response response = userResource.create(user);
System.out.println("Response: " + response.getStatusInfo());
System.out.println("Response: " + response.getStatus());
System.out.println("Response: " + response.getMetadata());
} catch (Exception e) {
System.out.println(ExceptionUtils.getStackTrace(e));
return new AjaxResponse("Fail", false);
}
return new AjaxResponse("Successful User Creation", true);
}
private static UserRepresentation getUserRepresentation(User newUser) {
UserRepresentation user = new UserRepresentation();
user.setEnabled(true);
user.setUsername(newUser.getUsername());
user.setFirstName(newUser.getFirstName());
user.setLastName(newUser.getLastName());
user.setEmail(newUser.getEmail());
CredentialRepresentation credentialRepresentation = new CredentialRepresentation();
credentialRepresentation.setTemporary(true);
credentialRepresentation.setType(CredentialRepresentation.PASSWORD);
credentialRepresentation.setValue(newUser.getUsername());
user.setCredentials(Collections.singletonList(credentialRepresentation));
}
我运行代码时得到的响应:
Username: USERNAME Correctly Identified here
Count: 98
Response: Conflict
Response: 409
Response: [Connection=keep-alive,Content-Length=46,Content-Type=application/json,Date=Tue, 03 Jul 2018 15:27:58 GMT,Server=WildFly/10,X-Powered-By=Undertow/1]`
一些想法: 我添加了计数器以识别整个连接是否正常工作。而且似乎返回的计数是正确的。所以我成功连接到keycloak,但在尝试创建用户时出了其他问题。
我的pom.xml中的依赖项:
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-servlet-filter-adapter</artifactId>
<version>3.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>3.2.1.Final</version> (Have also tried 3.1.0.Final and 3.2.0.Final)
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.4.Final</version>
</dependency>
我理解这些依赖关系也是相关的:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
其他信息:
我使用的Keycloak版本:3.2.0
可以在这里找到Keycloak API。
更多关于HTTP 409状态码的信息可以在这里找到。
我将非常感激任何帮助或指导。请注意,凭据和所有变量已经检查了一次、两次、三次,以及再次检查了10次。我一直在寻找一个完整的工作示例。但是大多数都没有指定所需的依赖项,因此我的代码失败了。
注意:我还使用Tomcat-8,并且在深入研究问题时,我发现可能需要一些特殊配置来适配Tomcat8。请注意,我的Web应用程序的身份验证登录和注销已经按预期工作。