在 Tomcat 中部署 Keycloak

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

介绍

注:可以从 github 下载源码

根据 Keycloak 文档,当前服务器安装仅在 Jboss 服务器(AS、Wildfly 和 EAP)中受支持,但是组织仅使用 JBOSS 服务器来托管 Keycloak 是没有意义的,实际上他们将运行 JBOSS(Tomcat)以外的服务器, 码头, Glassfish 等)

根据文档,对于其他服务器来说,这一定是一件容易的事,让我们来探索一下。

设置

如果你构建 keycloak服务器 ,并将war部署到Tomcat

问题 #1

您将收到以下错误,这肯定是 pom 问题

添加所需的依赖后,你可能会得到其他 ClassNotFoundExceptions,最后你可以在你的 pom 文件中添加以下依赖


 <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>

问题 #2

这次你会得到以下错误

问题是 ResteasyProviderFactory 没有 org.jboss.resteasy.core.Dispatcher 和 javax.servlet.ServletContext 的实例,因此它可以注入 KeycloakAppication

在这里解决这个问题就是我所做的。

确保在 pom 文件中也添加了以下依赖项


 <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>

问题 #3

解决上述问题后,您可能会遇到以下问题

必须正确配置 Tomcat 资源

对于 Tomcat 外部 JNDI 名称以 java:comp/env 开头

问题 #4

这次你会得到以下错误

将以下 jar 复制到您的 TOMCAT_HOME/lib 目录

使用以下内容创建 setenv.bat/setenv.sh 并将其复制到 TOMCAT_HOME/bin

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

事情现在应该像魅力一样运作

Keycloak 的两个问题

问题 #1:ResteasyProviderFactory 中没有 Dispather 和 ServletContext 的数据

问题 #2:keycloak-server.json 是从错误的地方加载的,即 classes/META-INF

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

参考 这个 TomEE 设置

相关文章