Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Deploying Keycloak In Tomcat

DZone's Guide to

Deploying Keycloak In Tomcat

Here's how to deploy Keycloak in Tomcat, including setup details and issue resolution.

· Integration Zone
Free Resource

Today’s data climate is fast-paced and it’s not slowing down. Here’s why your current integration solution is not enough. Brought to you in partnership with Liaison Technologies.

Introduction

Note: You can download the source from github

AS per the Keycloak documentation currently server installation is supported only in Jboss Servers (AS,Wildfly and EAP), However it does not make sense for Organizations to use JBOSS servers only to host Keycloak, Verily they would be running servers other than JBOSS (Tomcat, Jetty, Glassfish etc.)

kt14

As per the documentation it must be a easy task for the other servers, let’s explore.

Setup

If you do a build of the keycloak server, and deploy the war to Tomcat

kt14

 Issue #1

You will get the following error, which definitely seems to be pom issue

kt14

After adding required dependency you may get other ClassNotFoundExceptions, and finally you may add the following dependencies in your pom file

<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-dependencies-server-all</artifactId>
<version>${project.version}</version>
<type>pom</type>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-crypto</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>tjws</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>async-http-servlet-3.0</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
</dependency>

<!-- Email Test Servers -->
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
</dependency>

<!-- Encrypted ZIP -->
<dependency>
<groupId>de.idyl</groupId>
<artifactId>winzipaes</artifactId>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</dependency>
<!-- Older 1.5.10 binding required by embedded ApacheDS -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
<version>3.1.0</version>
</dependency>

</dependencies>

Issue #2

This time you would get the following error

kt14

The issue is that ResteasyProviderFactory does not have instance of org.jboss.resteasy.core.Dispatcher andjavax.servlet.ServletContext so that it can inject to KeycloakAppication

kt14

To fix the issue here is what I have done.

kt14

Make sure the following dependency is also added in the pom file

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
    <version>3.1.0</version>
</dependency>

Issue #3

After fixing the above issues you may get the following issue

kt14

The Tomcat Resource has to be configured properly

kt14

kt14

 For Tomcat external JNDI name starts with java:comp/env

Issue #4

This time you would get the following error

kt14

Copy the following jars to you TOMCAT_HOME/lib directory

kt14

Create setenv.bat/setenv.sh with the following content and copy it to TOMCAT_HOME/bin

CATALINA_OPTS=-Djavax.persistence.provider=org.hibernate.ejb.HibernatePersistence

Things Should be Working like a Charm Now

kt14

kt14

kt14

Two Issues on Keycloak

Issue #1 : No data for Dispather and ServletContext in ResteasyProviderFactory

Issue #2: keycloak-server.json is loaded from incorrect place, i.e, classes/META-INF

 config = Thread.currentThread().getContextClassLoader().getResource(“META-INF/keycloak-server.json”);

Refer this for TomEE setup

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
deploy ,keycloak ,tomcat ,integration ,ei ,entreprise integration

Published at DZone with permission of Mohammad Nadeem, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}