Custome event listner is not working

I have created a custom event listener in keycloak which was working fine in version 7 and stops working after upgrade to version 8.0.1.

Similar to this i have other providers for Email,Realm resource Provider,Theme resource provider are there, which is working perfectly without any issues,Only above mentioned provider having problem. Not able to figure out the cause, Can someone please help on this

The error is on line 17, that’s my guess based on the information you provided.
Without seeing some code if just a guessing game on what could be wrong.
Maybe the interface changed or with the upgrade the events are not active or your event listener is not setup.

For an event listener on the latest Keycloak version you can take a look at https://github.com/zonaut/keycloak-extensions

Hi zonaut,

Below is the code

Event Listner Provider Factory

package com.test.event.listner.factory;
import org.keycloak.Config;
import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.EventListenerProviderFactory;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import com.test.event.listner.provider.EventListnerProvider;
public class EventListnerFactory implements EventListenerProviderFactory {
@Override
public EventListenerProvider create(KeycloakSession keycloakSession) {
return new EventListnerProvider(keycloakSession);
}
@Override
public void init(Config.Scope scope) {
}
@Override
public void postInit(KeycloakSessionFactory keycloakSessionFactory) {
}
@Override
public void close() {
}
@Override
public String getId() {
return “customEventListner”;
}
}

I have created a file named org.keycloak.events.EventListenerProviderFactory inside META-INF/services and added below entry

com.test.event.listner.factory.EventListnerFactory

EventListenerProvider

package com.test.custom.event.listner.provider;
import org.jboss.logging.Logger;
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.AuthenticatedClientSessionModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserSessionModel;
import org.keycloak.models.UserSessionProvider;
import org.keycloak.services.managers.AuthenticationManager;
public class EventListnerProvider implements EventListenerProvider {
private static final Logger log = Logger.getLogger(“org.keycloak.events”);
private KeycloakSession keycloakSession;
public EventListnerProvider (KeycloakSession keycloakSession) {
this.keycloakSession = keycloakSession;
}
@Override
public void onEvent(Event event) {
if (event.getType() == EventType.LOGIN &&
event.getDetails().get(“auth_type”)==“code”) {
log.info(" client login ---- "+event.getSessionId());
}
}
@Override
public void onEvent(AdminEvent adminEvent, boolean b) {
log.info(“Admin Event Occurred:” + adminEvent.getOperationType());
}
@Override
public void close() {
}
}

I have packaged above code as a jar file and deployed to standalone/deployments directory. hope this information helps

Looks okay.
Can you take a look in your server info and see if it’s picked up. (auth/admin/master/console/#/server-info/providers)
Also take a look at Events > Config [tab] under your realm and see if it’s activated. In your case you should see ‘customEventListner’

Output of server info

Inside Events->Config this event listner is not displaying.

In version 7 it was showing without any issues. Not getting what makes wrong in version 8.0.1

Strange, are your providers build with Keycloak 8.0.1 or are you re-using already build ones?
Maybe try a more recent version of Keycloak?

Yes previously it was build using version 7 dependencies, now i have changed it to version 8 dependencies and builded fresh custom providers, even after that it is not working

Can you pls suggest a way forward

Can you try building the one in the repo I linked to and deploy that one and see what it does?
Or try a more recent Keycloak version.
I’m out of ideas after that, maybe it was a bug in that specific version.

Ok i will try latest version of keycloak and update

Hi @zonaut,

With updated version also not working. I have tried your code only… still no luck

Logs when deploying

15:23:33,806 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) WFLYDS0010: Scan found incompletely copied file content for deployment /opt/6d/app/keycloak-10.0.1/standalone/deployments/event-tes.jar. Deployment changes will not be processed until all content is complete.
15:23:44,070 INFO [org.jboss.as.repository] (DeploymentScanner-threads - 2) WFLYDR0001: Content added at location /opt/6d/app/keycloak-10.0.1/standalone/data/content/3f/e01366215c80f0e8d4f4502094df928e614f56/content
15:23:44,078 INFO [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0027: Starting deployment of “event-tes.jar” (runtime-name: “event-tes.jar”)
15:23:44,204 INFO [org.keycloak.subsystem.server.extension.KeycloakProviderDeploymentProcessor] (MSC service thread 1-4) Deploying Keycloak provider: event-tes.jar
15:23:44,344 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0010: Deployed “event-tes.jar” (runtime-name : “event-tes.jar”)

Please help. Not getting any clue whats happening

I’ll be happy to help if you can create a Github repo for your event listener.
That way I can try it out myself and take a look at it because we are getting nowhere like this.

Hi @zonaut,

Created Git Hub repo

Sorry but I’m not going to bother with this until you provide a repo which I can actually build. The example I provide in the repo is something you can start of.
The error you provided indicates that the jar isn’t build properly so maybe you should consider another project structure.
It seems you just copied my code to your project and tried that, I suggested building my repo and use that as a test.

Hi @zonaut,

Whats the difference if i take only the required portion from your repo and build ? I have issues with only event listener thats why i take out only that portion and builded.

I just simply builded using eclipse Export option which was working fine before.

Can you suggest me a project structure which i need to follow if you suspect issue with build ?

I already gave you an example of such a repo. the example I provide in https://github.com/zonaut/keycloak-extensions/tree/master/spi-event-listener can be build by everybody who has a JDK installed. You don’t even need a local Maven install if you use the wrapper in the root.

On first sight problems with your structure are the location of the META-INF folder and the name and location of the resource folder. Try to use a more generic and widely used project directory structure, especially when you share a git repo so anybody can build it and immediately use it without installing another IDE or changing things.

Look at the locations of the META-INF folder in my example and also the name and location of resources of the example. That’s a more widely used structure. The log line Scan found incompletely copied file content for deployment indicates that the jar you’ve build is not containing everything.

Also not everybody has Eclipse installed on their pc, I don’t and don’t want to install it.

1 Like

Hi @zonaut,

I got the issue. I just installed a fresh keycloak with new database and deployed above provider , it is working fine without any issues.

If i do an upgrade then only this issue is happening. Can you suggest why this behaviour when doing upgrade ?

I don’t see any reason why it should fail when you re-deploy after an upgrade.
If you can reproduce this error than I suggest you create an issue on https://issues.redhat.com/projects/KEYCLOAK/issues for it.