Created
December 20, 2019 12:50
-
-
Save adwait-thattey/72fd74473a3db8c1b61a856e0574326c to your computer and use it in GitHub Desktop.
KeyCloak Sample Event Listener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<jboss-deployment-structure> | |
<deployment> | |
<dependencies> | |
<module name="org.keycloak.keycloak-core"/> | |
<module name="org.keycloak.keycloak-server-spi"/> | |
<module name="org.keycloak.keycloak-server-spi-private"/> | |
<module name="org.keycloak.keycloak-services"/> | |
<module name="org.keycloak.keycloak-saml-core-public"/> | |
<module name="org.jboss.logging"/> | |
</dependencies> | |
</deployment> | |
</jboss-deployment-structure> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
com.coderdude.sampleeventlistenerprovider.provider.SampleEventListenerProviderFactory |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<parent> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-parent</artifactId> | |
<version>6.0.1</version> | |
</parent> | |
<name>Sample Event Listener</name> | |
<description/> | |
<modelVersion>4.0.0</modelVersion> | |
<artifactId>sample_event_listener</artifactId> | |
<packaging>jar</packaging> | |
<dependencies> | |
<dependency> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-core</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-server-spi</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-server-spi-private</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-services</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-saml-core-public</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.logging</groupId> | |
<artifactId>jboss-logging</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.spec.javax.ws.rs</groupId> | |
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>sample-event-listener</finalName> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.wildfly.plugins</groupId> | |
<artifactId>wildfly-maven-plugin</artifactId> | |
<configuration> | |
<skip>false</skip> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coderdude.sampleeventlistenerprovider.provider; | |
import org.keycloak.events.Event; | |
import org.keycloak.events.EventListenerProvider; | |
import org.keycloak.events.admin.AdminEvent; | |
import java.util.Map; | |
public class SampleEventListenerProvider implements EventListenerProvider { | |
public SampleEventListenerProvider() { | |
} | |
@Override | |
public void onEvent(Event event) { | |
System.out.println("Event Occurred:" + toString(event)); | |
} | |
@Override | |
public void onEvent(AdminEvent adminEvent, boolean b) { | |
System.out.println("Admin Event Occurred:" + toString(adminEvent)); | |
} | |
@Override | |
public void close() { | |
} | |
private String toString(Event event) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("type="); | |
sb.append(event.getType()); | |
sb.append(", realmId="); | |
sb.append(event.getRealmId()); | |
sb.append(", clientId="); | |
sb.append(event.getClientId()); | |
sb.append(", userId="); | |
sb.append(event.getUserId()); | |
sb.append(", ipAddress="); | |
sb.append(event.getIpAddress()); | |
if (event.getError() != null) { | |
sb.append(", error="); | |
sb.append(event.getError()); | |
} | |
if (event.getDetails() != null) { | |
for (Map.Entry<String, String> e : event.getDetails().entrySet()) { | |
sb.append(", "); | |
sb.append(e.getKey()); | |
if (e.getValue() == null || e.getValue().indexOf(' ') == -1) { | |
sb.append("="); | |
sb.append(e.getValue()); | |
} else { | |
sb.append("='"); | |
sb.append(e.getValue()); | |
sb.append("'"); | |
} | |
} | |
} | |
return sb.toString(); | |
} | |
private String toString(AdminEvent adminEvent) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("operationType="); | |
sb.append(adminEvent.getOperationType()); | |
sb.append(", realmId="); | |
sb.append(adminEvent.getAuthDetails().getRealmId()); | |
sb.append(", clientId="); | |
sb.append(adminEvent.getAuthDetails().getClientId()); | |
sb.append(", userId="); | |
sb.append(adminEvent.getAuthDetails().getUserId()); | |
sb.append(", ipAddress="); | |
sb.append(adminEvent.getAuthDetails().getIpAddress()); | |
sb.append(", resourcePath="); | |
sb.append(adminEvent.getResourcePath()); | |
if (adminEvent.getError() != null) { | |
sb.append(", error="); | |
sb.append(adminEvent.getError()); | |
} | |
return sb.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coderdude.sampleeventlistenerprovider.provider; | |
import org.keycloak.Config; | |
import org.keycloak.events.EventListenerProvider; | |
import org.keycloak.events.EventListenerProviderFactory; | |
import org.keycloak.models.KeycloakSession; | |
import org.keycloak.models.KeycloakSessionFactory; | |
public class SampleEventListenerProviderFactory implements EventListenerProviderFactory { | |
@Override | |
public EventListenerProvider create(KeycloakSession keycloakSession) { | |
return new SampleEventListenerProvider(); | |
} | |
@Override | |
public void init(Config.Scope scope) { | |
} | |
@Override | |
public void postInit(KeycloakSessionFactory keycloakSessionFactory) { | |
} | |
@Override | |
public void close() { | |
} | |
@Override | |
public String getId() { | |
return "sample_event_listener"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment