Get root Url in EventListenerProvider

Hi, I want to send a link of the realm to a designated user whenever a particular event happened in that realm, but I can’t seem to get rootUrl (i.e. localhost:8080).

Thanks in advance.

I’m not sure this will be populated in the event listener, as not all events happen within the context of a request. Have you tried something like the following:

  private String getServerUrl(KeycloakSession session) {
    URI u = session.getContext().getUri().getRequestUri();
    try {
      return new URI(u.getScheme(), null, u.getHost(), u.getPort(), "/", null, null).toString();
    } catch (URISyntaxException e) {
      throw new IllegalStateException(e);

Thanks for the reply. session.getContext() just cause the provider to hang every time it was called and timeout eventually, either with KeycloakSession generated by events or by calling KeycloakSessionFactory.create().
The only time that I could get it to work it to use the session I got from AuthenticationFlowContext.getSession() but again it would depend on the context to get the context thus rendering this useless.
Do you have any idea what is causing it?
Edit: apparently session.getContext() is not the cause but DefaultKeycloakContext.getUri() is the cause.

I think the problem is that it’s not populated because calls to the event listener are not necessarily done in the context of a request. There was a thread here a few months ago about somebody decorating the request in order to store it in another place, but I can’t find the thread now. Sorry, but I don’t have a ready solution for you.

Looks like I will have to softcode the Url somewhere the admin user can config then. Thanks for the help.