使用 JAVA 通过 REST API 集成 Keycloak 时在 toRepresentation() 处获取 404
Posted
技术标签:
【中文标题】使用 JAVA 通过 REST API 集成 Keycloak 时在 toRepresentation() 处获取 404【英文标题】:Getting 404 at toRepresentation() when integrating Keycloak via REST API using JAVA 【发布时间】:2021-05-14 05:37:56 【问题描述】:我正在尝试连接到在 localhost 上运行的 Keycloak 实例并使用 userid
查找特定用户。我已经在 Keycloak 中创建了相关的 Realm、用户等。
TestKeycloakConnection.java
package org.keycloak.pkg.test;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.admin.client.resource.UsersResource;
public class TestKeycloakConnection
public static final String KEYCLOAK_URL = "https://localhost/auth";
public static final String KEYCLOAK_REALM = "myreaml";
public static final String KEYCLOAK_USER = "admin";
public static final String KEYCLOAK_PASSWORD = "admin";
public static final String KEYCLOAK_ADMIN_CLI = "admin-cli";
public static void main(String[] args)
Keycloak keycloak = KeycloakBuilder
.builder()
.serverUrl(KEYCLOAK_URL)
.realm(KEYCLOAK_REALM)
.username(KEYCLOAK_USER)
.password(KEYCLOAK_PASSWORD)
.clientId(KEYCLOAK_ADMIN_CLI)
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
.build();
UsersResource usersResource = keycloak.realm(KEYCLOAK_REALM).users();
UserResource userResource = usersResource.get("8af231bd-0305-4921-a52d-6c0dda224e24");
////////////////////// GETTING 404 IN FOLLOWING LINE ////////////////////
System.out.println("userResource:"+userResource.toRepresentation().getUsername());
pom.xml
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>12.0.2</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.15.0.Alpha1</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.26.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.1.0.Final</version>
</dependency>
错误堆栈:
线程“主”javax.ws.rs.ProcessingException 中的异常: javax.ws.rs.NotFoundException:HTTP 404 未在 org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:436) 在 org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:106) 在 org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76) 在 com.sun.proxy.$Proxy27.toRepresentation(未知来源) org.keycloak.pkg.providers.config.TestKeycloakConnection.main(TestKeycloakConnection.java:41) 原因:javax.ws.rs.NotFoundException: HTTP 404 Not Found at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:200)
类似的问题:
https://keycloak.discourse.group/t/404-when-calling-torepresentation/3606
【问题讨论】:
【参考方案1】:问题在于这部分:
Keycloak keycloak = KeycloakBuilder
.builder()
.serverUrl(KEYCLOAK_URL)
.realm(KEYCLOAK_REALM) // <----- this is wrong
.username(KEYCLOAK_USER)
.password(KEYCLOAK_PASSWORD)
.clientId(KEYCLOAK_ADMIN_CLI)
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
.build();
您应该将master
传递给.realm(..)
,因为这通常是客户端admin-cli
所在的位置。
一个正在运行的解决方案:
package org.keycloak.pkg.test;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.admin.client.resource.UsersResource;
public class TestKeycloakConnection
public static final String KEYCLOAK_URL = "https://localhost/auth";
public static final String KEYCLOAK_REALM = "myreaml";
public static final String KEYCLOAK_USER = "admin";
public static final String KEYCLOAK_PASSWORD = "admin";
public static final String KEYCLOAK_ADMIN_CLI = "admin-cli";
public static void main(String[] args)
Keycloak keycloak = KeycloakBuilder
.builder()
.serverUrl(KEYCLOAK_URL)
.realm("master")
.username(KEYCLOAK_USER)
.password(KEYCLOAK_PASSWORD)
.clientId(KEYCLOAK_ADMIN_CLI)
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
.build();
UsersResource usersResource = keycloak.realm(KEYCLOAK_REALM).users();
UserResource userResource = usersResource.get("8af231bd-0305-4921-a52d-6c0dda224e24");
System.out.println("userResource:"+userResource.toRepresentation().getUsername());
【讨论】:
以上是关于使用 JAVA 通过 REST API 集成 Keycloak 时在 toRepresentation() 处获取 404的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 Java/Kotlin API 从 KeyCloak 请求 JWT 令牌
将 REST Web API 应用程序与 SignalR 集成?