从终端运行Keycloak的Spring Boot/Security Classloader问题 [英] Spring Boot / Security classloader issues with Keycloak run from terminal

查看:156
本文介绍了从终端运行Keycloak的Spring Boot/Security Classloader问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring Boot和Spring Security与Keycloak结合使用.构建工具是gradle.

I use Spring Boot and Spring Security in combination with Keycloak. The build tool is gradle.

当我运行./gradlew bootRun时,该应用程序可以正常运行.如果我使用生成的胖子罐(即java -jar myapp.jar),则该应用程序将启动,但是当该应用程序尝试调用某些keyloak东西时遇到异常:

When I run ./gradlew bootRun the application works flawless. If I use the resulting fat jar (i.e. java -jar myapp.jar) the application will boot but I encounter an exception when the application tries to invoke some keyloak stuff:

java.lang.IllegalArgumentException: org.keycloak.admin.client.resource.RealmsResource referenced from a method is not visible from class loader
    at java.base/java.lang.reflect.Proxy$ProxyBuilder.ensureVisible(Proxy.java:851) ~[na:na]
    at java.base/java.lang.reflect.Proxy$ProxyBuilder.validateProxyInterfaces(Proxy.java:682) ~[na:na]
    at java.base/java.lang.reflect.Proxy$ProxyBuilder.<init>(Proxy.java:628) ~[na:na]
    at java.base/java.lang.reflect.Proxy.lambda$getProxyConstructor$1(Proxy.java:426) ~[na:na]
    at java.base/jdk.internal.loader.AbstractClassLoaderValue$Memoizer.get(AbstractClassLoaderValue.java:327) ~[na:na]
    at java.base/jdk.internal.loader.AbstractClassLoaderValue.computeIfAbsent(AbstractClassLoaderValue.java:203) ~[na:na]
    at java.base/java.lang.reflect.Proxy.getProxyConstructor(Proxy.java:424) ~[na:na]
    at java.base/java.lang.reflect.Proxy.newProxyInstance(Proxy.java:999) ~[na:na]
    at org.jboss.resteasy.client.jaxrs.ProxyBuilder.proxy(ProxyBuilder.java:79) ~[resteasy-client-3.1.4.Final.jar!/:3.1.4.Final]
    at org.jboss.resteasy.client.jaxrs.ProxyBuilder.build(ProxyBuilder.java:131) ~[resteasy-client-3.1.4.Final.jar!/:3.1.4.Final]
    at org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget.proxy(ClientWebTarget.java:93) ~[resteasy-client-3.1.4.Final.jar!/:3.1.4.Final]
    at org.keycloak.admin.client.Keycloak.realms(Keycloak.java:114) ~[keycloak-admin-client-3.4.3.Final.jar!/:3.4.3.Final]
    at org.keycloak.admin.client.Keycloak.realm(Keycloak.java:118) ~[keycloak-admin-client-3.4.3.Final.jar!/:3.4.3.Final]

所以我发现在终端中启动应用程序一定有问题.我找到了此官方网站,解释了如何从终端运行Spring应用程序.因此,我尝试了所有解决方案,包括:

So I figured out that there must be something wrong how I start the application in the terminal. I found this official site, which explains how to run an Spring application from terminal. So I tried all of the solutions including:

$ unzip -q myapp.jar
$ java org.springframework.boot.loader.JarLauncher

但是我遇到了同样的错误.

but I get the same error.

经过2天的搜索和实验后,我没有任何想法.

After 2 days of searching and experimenting I'm out of any ideas.

所以我的问题基本上是: 有人知道如何解决此问题吗?

So my question is basically: Does anyone have any idea how to solve this problem?

我对类加载器的了解也很有限-因此,对此方向的任何实用提示也将受到欢迎.

My knowledge regarding the classloader is also limited - so any pratical hints in this direction are very welcomed as well.

编辑(添加了build.gradle): 有一些Jackson问题,这就是为什么直接包含Spring Web依赖项的原因(没有启动器,因此没有Jackson). 这是build.gradle:

EDIT (added build.gradle): There were some Jackson problems that's why the Spring web dependencies are included directly (without the starter and therefor without Jackson). Here is the build.gradle:

buildscript {
   ext {
      kotlinVersion = '1.2.20'
      springBootVersion = '1.5.7.RELEASE'
      keycloakVersion = '3.4.3.Final'
      restEasyClientVersion = '3.1.4.Final'
      postgresServerVersion = '10.0'
      postgresJdbcDriverVersion = '42.1.4'
      spekVersion = '1.1.5'
      jacksonVersion = '2.8.10'
      javaxWsRsVersion = '2.1'

      logbackVersion = '1.2.3'
      slf4jVersion = '1.7.25'
   }
   repositories {
      mavenCentral()
      jcenter()
      //spring dev
      maven { url 'https://repo.spring.io/snapshot' }
      maven { url 'https://repo.spring.io/milestone' }
      //Kotlin dev
      maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.2' }
      //gradle plugins
      maven { url "https://plugins.gradle.org/m2/" }
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
      classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
      classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
      classpath('com.bmuschko:gradle-docker-plugin:3.2.0')

      //for tests
      classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
   }
}

allprojects {

   repositories {
      mavenCentral()
      jcenter()
      //spring dev
      maven { url 'https://repo.spring.io/snapshot' }
      maven { url 'https://repo.spring.io/milestone' }
   }

}

subprojects {

   repositories {
      maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.2' }
      // for tests
      maven { url "http://dl.bintray.com/jetbrains/spek" }
   }

   // for kotlin
   apply plugin: 'kotlin'
   // for tests
   apply plugin: 'org.junit.platform.gradle.plugin'

   apply plugin: 'kotlin-spring'
   apply plugin: 'org.springframework.boot'
   apply plugin: 'io.spring.dependency-management'

   // for tests
   junitPlatform {
      filters {
         engines {
            include 'spek'
         }
      }
   }

   sourceCompatibility = 1.9

   dependencies {
      // kotlin
      compile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
      compile("org.jetbrains.kotlin:kotlin-test:${kotlinVersion}")
      compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
      // jackson
      compile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
      compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
      compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:${jacksonVersion}"
      compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:${jacksonVersion}"
      compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2"

      // Java WS RS
      compile "javax.ws.rs:javax.ws.rs-api:${javaxWsRsVersion}"
      // for tests
      testCompile "org.jetbrains.spek:spek-api:${spekVersion}"
      testRuntime "org.jetbrains.spek:spek-junit-platform-engine:${spekVersion}"
      testCompile ("org.jetbrains.spek:spek-api:${spekVersion}") {
         exclude group: 'org.jetbrains.kotlin'
      }
      testRuntime ("org.jetbrains.spek:spek-junit-platform-engine:${spekVersion}") {
         exclude group: 'org.junit.platform'
         exclude group: 'org.jetbrains.kotlin'
      }
      // spring security
      compile('org.springframework.boot:spring-boot-starter-security')
      testCompile('org.springframework.security:spring-security-test')
      // begin: spring web without jackson
      compile('org.springframework.boot:spring-boot-starter')
      compile('org.springframework.boot:spring-boot-starter-tomcat')
      compile('org.springframework:spring-web')
      compile('org.springframework:spring-webmvc')
      testCompile('org.springframework.boot:spring-boot-starter-test')
      // end: spring web without jackson
      // Keycloak
      compile("org.keycloak:keycloak-spring-security-adapter:${keycloakVersion}")
      compile("org.keycloak:keycloak-spring-boot-adapter:${keycloakVersion}")
      compile("org.keycloak:keycloak-tomcat8-adapter:${keycloakVersion}")
      compile("org.keycloak:keycloak-admin-client:${keycloakVersion}")
      compile("org.jboss.resteasy:resteasy-client:${restEasyClientVersion}")
      compile("org.jboss.resteasy:resteasy-jackson2-provider:${restEasyClientVersion}")

   }

   compileKotlin {
      kotlinOptions.jvmTarget = '1.8'
      kotlinOptions.allWarningsAsErrors = true
   }
   compileTestKotlin {
      kotlinOptions.jvmTarget = '1.8'
   }
}

推荐答案

问题是CompletableFuture.supplyAsync使用的ForkJoinPool.commonPool的基础类加载器.

The problem was the underlying class loader of ForkJoinPool.commonPool which is used by CompletableFuture.supplyAsync.

因为该问题和解决方案很复杂请参阅我的其他问题以便更好地理解.

Because the problem and the solution is complex please refer to my other question for better understanding.

该问题仅在交叉引用时有效(可能希望将其他问题引向正确的解决方案).

This question is only kept alive for this cross reference (and may hopefully lead others to the correct solution).

这篇关于从终端运行Keycloak的Spring Boot/Security Classloader问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆