问题描述
我正在使用Keycloak保护我的Spring应用程序(该应用程序在Keycloak中注册为my_app
客户端).现在,我想检索该客户端的所有活动会话.如果我可以使用keycloak-admin-client做到这一点,那就太好了,因为我不知道如何在Java中使用Admin Rest API ...
I'm using Keycloak to secure my Spring app (which is registered in Keycloak as my_app
client). Now I want to retrieve all active sessions of that client. Would be great if I could do it using keycloak-admin-client, because I couldn't figure out how to use Admin Rest API in java...
到目前为止,我已经尝试过:
So far I've tried this:
Keycloak keycloak=KeycloakBuilder.builder()
.serverUrl("http://localhost:8180/auth")
.realm("master")
.username("admin")
.password("admin")
.clientId("admin-cli")
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()).build();
ClientResource client=keycloak.realm("MY_REALM").clients().get("my_app").getUserSessions(1,100);
但是会引发404错误.
But it throws 404 error.
推荐答案
List clientRepresentations=keycloak.realm("MY_REALM").clients().findByClientId("my_app"); ClientRepresentation representation=clientRepresentations.get(0);ClientResource resource=keycloak.realm("MY_REALM").clients().get(representation.getId());
List clientRepresentations=keycloak.realm("MY_REALM").clients().findByClientId("my_app"); ClientRepresentation representation=clientRepresentations.get(0);ClientResource resource=keycloak.realm("MY_REALM").clients().get(representation.getId());
成功了.
这将检索客户端中的所有活动会话:
And this retrieves all active sessions in client:
List sessions=resource.getUserSessions(0,1000);
List sessions=resource.getUserSessions(0,1000);
这篇关于Keycloak:检索所有可用的客户端会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!