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

前端之家收集整理的这篇文章主要介绍了java – 通过keycloak管理客户端在keycloak中创建用户返回IllegalArgumentException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过keycloak管理客户端创建一个用户,但我得到:

@L_404_0@.lang.IllegalArgumentException: RESTEASY003720: path param realm has not been provided by the parameter map

这是我的keycloak的bean:

@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();
}

我使用此代码调用keycloak:

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)

我的stacktrace看起来像这样:

java.lang.IllegalArgumentException: RESTEASY003720: path param realm has not been provided by the parameter map
at org.jboss.resteasy.specimpl.ResteasyUriBuilder.replaceParameter(ResteasyUriBuilder.java:659)
at org.jboss.resteasy.specimpl.ResteasyUriBuilder.buildString(ResteasyUriBuilder.java:581)
at org.jboss.resteasy.specimpl.ResteasyUriBuilder.buildFromValues(ResteasyUriBuilder.java:780)
at org.jboss.resteasy.specimpl.ResteasyUriBuilder.build(ResteasyUriBuilder.java:772)
at org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget.getUri(ClientWebTarget.java:108)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.createRequest(ClientInvoker.java:124)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:104)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy240.grantToken(Unknown Source)
at org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:89)
at org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:69)
at 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',name: 'resteasy-jackson2-provider',version: '3.1.4.Final'

解决方法

你在使用Spring Boot吗?

我已经为自己尝试过你的场景,对我来说效果很好(在keycloak 4.0.0和Spring Boot 1.5.10.RELEASE上测试过).

我做了一个改变你创建Bean Keycloak的方式(我的代码来自官方docs).确保领域是master,client_id是admin-cli.

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

我的代码

服务
Code

控制器Code

缺点here

原文链接:https://www.f2er.com/java/129388.html

猜你在找的Java相关文章