11

I'm creating a Keycloak extension with dependencies. I added the entry on the pom.xml like this:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160810</version>
</dependency>

Then I deployed it to Keycloak:

mvn clean install wildfly:deploy

But when I run it, I got the error:

org.jboss.resteasy.spi.UnhandledException: java.lang.NoClassDefFoundError: org/json/JSONObject
Caused by: java.lang.NoClassDefFoundError: org/json/JSONObject
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject from [Module "deployment.keycloak-authenticator.jar" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:412)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:400)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
    ... 66 more

How to add dependencies to extensions in Keycloak?

rigon
  • 1,310
  • 4
  • 15
  • 37

2 Answers2

12

You have to create your SPI dependencies as jboss modules.

Steps:

  1. Add a jboss-deployment-structure.xml file in src/main/resources/META-INF directory or your SPI with something like this (oficial documentation):

    <jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="org.json.json" />
        </dependencies>
    </deployment>
    </jboss-deployment-structure>
    
  2. Make $KEYCLOAK_HOME/modules/system/layers/base/org/json/json/main directory

  3. Add json-20160810.jar in created dir

  4. Add a module.xml file in same dir with this content:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <module xmlns="urn:jboss:module:1.5" name="org.json.json">
        <properties>
            <property name="jboss.api" value="private"/>
        </properties>
    
        <resources>
            <resource-root path="json-20160810.jar"/>
        </resources>
    
        <dependencies>
        </dependencies>
    </module>
    
  5. Compile your SPI

  6. Restart keycloak

  7. Redeploy your SPI

Yeray Rodriguez
  • 307
  • 5
  • 14
  • There a better way, deploy as a EAR archive. This project shows how to: https://github.com/stianst/keycloak-experimental/tree/master/fido-u2f – rigon Dec 04 '18 at 18:26
5

There is a better way, deploy as a EAR archive. This project shows how to: https://github.com/stianst/keycloak-experimental/tree/master/fido-u2f

rigon
  • 1,310
  • 4
  • 15
  • 37