springspring-bootdockerdocker-composekeycloak

Keycloak custom event listener not deploying


I'm trying to create a custom event listener and faced a problem that while I running the container with keycloak there is not any logging info about deploying of mine .jar file of a project.

My sequence of actions before running the container with keycloak:

mvn clean package -> build -t custom-keycloak . -> docker-compose up

Besides implementations bellow I have a file org.keycloak.events.EventListenerProviderFactory that contains directory of mine implementation of event listener provider factory - SimpleEventListenerProvider.

Event listener provider implementation

@Component
public class SimpleEventListenerProvider implements EventListenerProvider {

    @Override
    public void onEvent(Event event) {
    }

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

    @Override
    public void close() {

    }
}

Event listener provider factory implementation

@Component
@RequiredArgsConstructor
public class SimpleEventListenerProviderFactory implements EventListenerProviderFactory {
    private final SimpleEventListenerProvider provider;

    @Override
    public EventListenerProvider create(KeycloakSession keycloakSession) {
        return provider;
    }

    @Override
    public void init(Config.Scope scope) {

    }

    @Override
    public void postInit(KeycloakSessionFactory keycloakSessionFactory) {

    }

    @Override
    public void close() {

    }

    @Override
    public String getId() {
        return "session-restrictor";
    }
}

Dockerfile

FROM quay.io/keycloak/keycloak:19.0.0

ARG APPLICATION_JAR=target/*.jar

COPY ${APPLICATION_JAR} /opt/keycloak/providers

RUN /opt/keycloak/bin/kc.sh build  show-config --metrics-enabled=true

Docker compose

version: "3.8"

services:
  postgres:
    container_name: postgres
    image: postgres:latest
    ports:
      - '5432:5432'
    volumes:
      - /home/lib/v1:/var/lib/v1
    environment:
      POSTGRES_USERNAME: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: data

  keycloak:
    container_name: keycloak
    image: custom-keycloak:latest
    volumes:
      - /home/lib/keycloak/v1:/var/lib/keycloak/v1
    environment:
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
      KEYCLOAK_MANAGEMENT_PASSWORD: admin
      KEYCLOAK_DATABASE_PORT: 5432
      KEYCLOAK_DATABASE_HOST: postgres
      KEYCLOAK_DATABASE_NAME: data
      KEYCLOAK_CREATE_ADMIN_USER: 'true'
      KEYCLOAK_DATABASE_USER: postgres
      KEYCLOAK_DATABASE_PASSWORD: postgres
    entrypoint: ["/opt/keycloak/bin/kc.sh", "start-dev", "--features=preview"]
    depends_on:
      postgres:
        condition: service_started
    ports:
      - '8080:8080'
    restart: unless-stopped

Solution

  • I experienced the same issue, unfortunately the documentation on configuring providers is very sparse and does not mention the important notice that the src/resources/META-INF folder must contain a text file with your SimpleEventListenerProviderFactory.

    The solution would be to create the folder src/resources/META-INF/ and place a plain text file as follows.

    org.keycloak.events.EventListenerProviderFactory:

    your.package.name.SimpleEventListenerProviderFactory
    

    Don't forget to build the configuration again with kc.sh build before running kc.sh start

    This costed me some hours to figure out. But now it finally shows in the keycloak admin page. Due to lack of better examples, I can only provide my implementation. Also notice the xml file:

    jboss-deployment-structure.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-deployment-structure>
        <deployment>
            <dependencies>
                <module name="org.keycloak.keycloak-services" />
            </dependencies>
        </deployment>
    </jboss-deployment-structure>