通过keycloak管理客户端在keycloak中创建用户返回IllegalArgumentException

时间:2018-08-01 05:43:05

标签: java spring-boot resteasy keycloak

我想通过keycloak admin客户端创建一个用户,但是我得到:

  

java.lang.IllegalArgumentException:RESTEASY003720:参数映射未提供路径参数领域

这是我的钥匙斗篷豆:

@Bean
Keycloak keycloak() {
return KeycloakBuilder
    .builder()
    .serverUrl(localhost:9080/auth)
    .realm(REALM)
    .clientId(CLIENT_ID)
    .username(USERNAME)
    .password(PASSWORD)
    .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
    .build();
}

我使用以下代码调用密钥斗篷:

CredentialRepresentation credentialRepresentation = new 
CredentialRepresentation();
credentialRepresentation.setType(CredentialRepresentation.PASSWORD);
credentialRepresentation.setValue(password);
UserRepresentation userRepresentation = new UserRepresentation();
userRepresentation.setUsername(username);
userRepresentation.setFirstName(firstName);
userRepresentation.setLastName(lastName);
userRepresentation.setEnabled(true);
userRepresentation.setCredentials(
    Arrays.asList(credentialRepresentation));
keycloak.realm(REALM).users().create(userRepresentation);

keycloak和keycloak管理客户端都是相同的版本(4.0.0.Final)

我的堆栈跟踪如下:

  

java.lang.IllegalArgumentException:RESTEASY003720:参数映射未提供路径参数领域   在org.jboss.resteasy.specimpl.ResteasyUriBuilder.replaceParameter(ResteasyUriBuilder.java:659)   在org.jboss.resteasy.specimpl.ResteasyUriBuilder.buildString(ResteasyUriBuilder.java:581)   在org.jboss.resteasy.specimpl.ResteasyUriBuilder.buildFromValues(ResteasyUriBuilder.java:780)   在org.jboss.resteasy.specimpl.ResteasyUriBuilder.build(ResteasyUriBuilder.java:772)   在org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget.getUri(ClientWebTarget.java:108)   在org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.createRequest(ClientInvoker.java:124)   在org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:104)   在org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)   在com.sun.proxy。$ Proxy240.grantToken(未知来源)   在org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:89)   在org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:69)   在org.mycompany.usermanagement.service.KeycloakService.createUserInSSO(KeycloakService.java:45)

这是我的build.gradle

compile group: 'org.keycloak', name: 'keycloak-admin-client', version: '4.0.0.Final'
compile group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.1.4.Final'
compile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '3.1.4.Final'
compile group: 'org.jboss.resteasy', name: 'resteasy-jackson2-provider', version: '3.1.4.Final'

1 个答案:

答案 0 :(得分:4)

您正在使用Spring Boot吗?

我已经为自己尝试过您的方案,并且对我来说效果很好(在keycloak 4.0.0和Spring Boot 1.5.10.RELEASE上进行了测试)。

我对您创建Bean Keycloak的方式做了1处更改(我的代码来自官方docs)。确保realmmaster,而client_idadmin-cli

@Bean
Keycloak initKeycloakWithAdminRole() {
    return Keycloak.getInstance(
            "http://localhost:8080/auth",
            "master",
            "admin",
            "admin",
            "admin-cli");


我的代码

服务 Code

控制器Code

缺陷here