Search code examples
jettyembedded-jettykeycloak

Keycloak conflict (core and adapter)


When trying to run my webapplication in both an embedded jetty (locally) and a 'normal' jetty instance (remotely), I think I encounter some class collision.

java.lang.ClassCastException: 
org.keycloak.adapters.RefreshableKeycloakSecurityContext cannot be cast to org.keycloak.KeycloakSecurityContext

The class KeycloakSecurityContext is both defined in the keycloak-jetty-adapter dependency and in the keycloak-core dependency in my project. (same version, 3.4.3 Final)

I've tried using scope 'provided' on the adapter or excluding the keycloak-core dependency from my adapter, but to no effect. Any suggestions how to handle this combination?


Solution

  • I had similar issues with Keycloak. You need to make sure that libraries are not duplicated.

    1. You download from Keycloak the adapters and place them under tomcat/lib in tomcat/wildfly.
    2. In pom.xml you need to specify the tag without versions. This will make sure that "dependencies that are required for compiling the project code, but that will be provided at runtime by a container running the code".
    3. (For IntelliJ) To make sure that your project can import Keycloak specific classes you need add them into your project. So CTRL+Shift+Alt+S -> under libraries you need to add the same jars you added at point 1.