Christophe de Vienne
Christophe de Vienne

Reputation: 331

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

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

Upvotes: 7

Views: 12866

Answers (2)

Alator
Alator

Reputation: 508

To do the same thing as @Christophe de Vienne using Gradle:

build.gradle

configurations {
    bundleLib
}

dependencies {
    //...
    compile "org.keycloak:keycloak-services:$keycloakVersion"
}

jar {
    from {
        configurations.bundleLib.collect { it.isDirectory() ? it : zipTree(it) }
    }
    manifest {
        attributes(
                'Dependencies'   : "org.keycloak.keycloak-services"
        )
    }
}

Upvotes: 2

Christophe de Vienne
Christophe de Vienne

Reputation: 331

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)

Upvotes: 26

Related Questions