Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycloak extension with dependencies

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?

like image 489
rigon Avatar asked Sep 13 '17 19:09

rigon


2 Answers

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

like image 85
Yeray Rodriguez Avatar answered Oct 08 '22 17:10

Yeray Rodriguez


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

like image 31
rigon Avatar answered Oct 08 '22 18:10

rigon