Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError in a provider jar when using a class from org.keycloak.authentication.authenticators.broker.util

Tags:

java

jar

keycloak

I am writing an Authenticator provider for keycloak, that I package as a .jar. As soon as it uses a class from keycloak-services, I get a NoClassDefFoundError. I get the same error when the provider is deployed via "mvn wildfly:deploy".

I must be missing something, but I rarely do java code and I am clueless at this point.

I defined the dependencies in pom.xml, and tried both 'provided' and 'compile' as scope:

    <dependencies>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-core</artifactId>
            <scope>provided</scope>
            <version>${keycloak.version}</version>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-server-spi</artifactId>
            <scope>provided</scope>
            <version>${keycloak.version}</version>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-server-spi-private</artifactId>
            <scope>provided</scope>
            <version>${keycloak.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <scope>provided</scope>
            <version>3.1.4.GA</version>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-services</artifactId>
            <scope>provided</scope>
            <version>${keycloak.version}</version>
        </dependency>
    </dependencies>

I get the error as soon as I add the following code to the authenticate function:

    AuthenticationSessionModel authSession = context.getAuthenticationSession();
    SerializedBrokeredIdentityContext serializedCtx =
        SerializedBrokeredIdentityContext.readFromAuthenticationSession(
            authSession, "BROKERED_CONTEXT");

The error I get:

20:05:03,844 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-6) Uncaught server error: java.lang.NoClassDefFoundError: org/keycloak/authentication/authenticators/broker/util/SerializedBrokeredIdentityContext
like image 221
Christophe de Vienne Avatar asked Dec 02 '22 09:12

Christophe de Vienne


1 Answers

Because wildfly isolates the classloaders, I had to declare the dependencies in the META-INF/MANIFEST.MF file.

To do that, I added the following code to my pom.xml file:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Dependencies>org.keycloak.keycloak-services</Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

(The solution was given to me on the keycloak user list: https://lists.jboss.org/pipermail/keycloak-user/2019-September/019108.html)

like image 98
Christophe de Vienne Avatar answered Dec 04 '22 00:12

Christophe de Vienne