Keycloak 24.0.2 rejected useful getUserById

Hi everyone, i am building eventListener in keycloak 24.0.2, now the event listener is in the admin console keycloak working, but i tried extract the username login, but when i tried use getUserById i have an error ERROR [org.keycloak.events.EventBuilder] (executor-thread-5) Failed to send type to it.aboutbits.SessionRestrictorEventListenerProvider@61eca458: java.lang.NoSuchMethodError: ‘org.keycloak.models.UserModel org.keycloak.models.UserProvider.getUserById(java.lang.String, org.keycloak.models.RealmModel)’
i show you the complete code

package it.aboutbits;

import org.keycloak.events.Event;
import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.EventType;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserSessionModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SessionRestrictorEventListenerProvider implements EventListenerProvider {

private static final Logger log = LoggerFactory.getLogger(SessionRestrictorEventListenerProvider.class);

private final KeycloakSession session;

public SessionRestrictorEventListenerProvider(KeycloakSession session) {
    this.session = session;
}

@Override
public void onEvent(Event event) {
    if (event.getType().equals(EventType.LOGIN)) {
        String realmId = event.getRealmId();
        RealmModel realm = session.realms().getRealm(realmId);
        String userId = event.getUserId();
        log.info(String.valueOf(realmId));
        log.info(String.valueOf(realm));
        log.info(String.valueOf(userId));

        UserModel user = session.users().getUserById(userId);
        if (user != null) {
            log.info("Usuario encontrado: " + user.getUsername());
        } else {
            log.warn("Usuario no encontrado con ID: " + userId);
        }
    }
}

private UserSessionModel findUserSession(String userId, String sessionId) {
    return session.sessions().getUserSessions(session.getContext().getRealm(), session.users().getUserById(userId, session.getContext().getRealm()))
            .stream()
            .filter(session -> session.getId().equals(sessionId))
            .findFirst()
            .orElse(null);
}

@Override
public void onEvent(AdminEvent adminEvent, boolean b) {

}

@Override
public void close() {

}

}

i need help, really i dont know what to do.

Are you compiling against an older version of Keycloak? That method doesn’t exist any more for quite some time. It is now realm scoped.

You can find an example for a current implementation here: keycloak-last-login-event-listener/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java at main · ThoreKr/keycloak-last-login-event-listener · GitHub

Thanks for your answer, now the event listening is working good, but i have another problem, i dont know how can i consume this username, i try to use httpurlconnection public static void postUsername(String username) {
try {
URL url = new URL(“http://localhost:8081/receive-username”);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-Type”, “application/json; utf-8”);

        String requestBody = "{\"username\": \"" + username + "\"}";

        OutputStream os = conn.getOutputStream();
        os.write(requestBody.getBytes());
        os.flush();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            log.info("Se envio username correctamente");
        } else {
            log.warn("No se pudo enviar el username");
        }

        conn.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
} this api send, the username from another api, that api receive and process data, but when try send data, appear this error 2024-04-01 17:03:57 java.net.ConnectException: Connection refused

2024-04-01 17:03:57 at java.base/sun.nio.ch.Net.connect0(Native Method)
2024-04-01 17:03:57 at java.base/sun.nio.ch.Net.connect(Net.java:579)
2024-04-01 17:03:57 at java.base/sun.nio.ch.Net.connect(Net.java:568)
2024-04-01 17:03:57 at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:593)
2024-04-01 17:03:57 at java.base/java.net.Socket.connect(Socket.java:633)
2024-04-01 17:03:57 at java.base/java.net.Socket.connect(Socket.java:583)
2024-04-01 17:03:57 at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:183)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:533)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:638)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.(HttpClient.java:281)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:386)
2024-04-01 17:03:57 at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:408)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1309)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1242)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1128)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1057)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1430)
2024-04-01 17:03:57 at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1401)
2024-04-01 17:03:57 at it.aboutbits.ExternalAPIClient.postUsername(ExternalAPIClient.java:27)
2024-04-01 17:03:57 at it.aboutbits.SessionRestrictorEventListenerProvider.onEvent(SessionRestrictorEventListenerProvider.java:33)
2024-04-01 17:03:57 at org.keycloak.events.EventBuilder.sendNow(EventBuilder.java:266)
2024-04-01 17:03:57 at org.keycloak.events.EventBuilder.send(EventBuilder.java:253)
2024-04-01 17:03:57 at org.keycloak.events.EventBuilder.success(EventBuilder.java:221)
2024-04-01 17:03:57 at org.keycloak.services.managers.AuthenticationManager.finishedRequiredActions(AuthenticationManager.java:1031)
2024-04-01 17:03:57 at org.keycloak.authentication.AuthenticationProcessor.authenticationComplete(AuthenticationProcessor.java:1133)
2024-04-01 17:03:57 at org.keycloak.authentication.AuthenticationProcessor.authenticationAction(AuthenticationProcessor.java:995)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService.processFlow(LoginActionsService.java:364)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService.processAuthentication(LoginActionsService.java:335)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService.authenticate(LoginActionsService.java:327)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService.authenticateForm(LoginActionsService.java:392)
2024-04-01 17:03:57 at org.keycloak.services.resources.LoginActionsService$quarkusrestinvoker$authenticateForm_32b8e198ac3110abd1d5774e83a4cf87858129f4.invoke(Unknown Source)
2024-04-01 17:03:57 at org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:29)
2024-04-01 17:03:57 at io.quarkus.resteasy.reactive.server.runtime.QuarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:141)
2024-04-01 17:03:57 at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:147)
2024-04-01 17:03:57 at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:582)
2024-04-01 17:03:57 at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
2024-04-01 17:03:57 at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
2024-04-01 17:03:57 at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
2024-04-01 17:03:57 at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
2024-04-01 17:03:57 at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
2024-04-01 17:03:57 at java.base/java.lang.Thread.run(Thread.java:840) i think this is for the keycloak security, but i dont know how disabled ir or another way to extract that data. you can help me please?