简体   繁体   English

Tomcat ClassCastException 由于 ClassLoaders

[英]Tomcat ClassCastException due to ClassLoaders

I am having some trouble implementing my own authentication filter in Tomcat.我在 Tomcat 中实现自己的身份验证过滤器时遇到了一些麻烦。 When I deploy the project I get the following log:当我部署项目时,我得到以下日志:

Caused by: java.lang.ClassCastException: class com.somename.sec.sso.camunda.KeycloakSSOAuthenticationProvider cannot be cast to class org.camunda.bpm.engine.rest.security.auth.AuthenticationProvider (com.somename.sec.sso.camunda.KeycloakSSOAuthenticationProvider is in unnamed module of loader java.net.URLClassLoader @6fc6f14e; org.camunda.bpm.engine.rest.security.auth.AuthenticationProvider is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @7a498efa) Caused by: java.lang.ClassCastException: class com.somename.sec.sso.camunda.KeycloakSSOAuthenticationProvider cannot be cast to class org.camunda.bpm.engine.rest.security.auth.AuthenticationProvider (com.somename.sec.sso. camunda.KeycloakSSOAuthenticationProvider is in unnamed module of loader java.net.URLClassLoader @6fc6f14e; org.camunda.bpm.engine.rest.security.auth.AuthenticationProvider is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @7a498efa)

Which is weird, since my KeycloakSSOAuthenticationProvider implements AuthenticationProvider.这很奇怪,因为我的 KeycloakSSOAuthenticationProvider 实现了 AuthenticationProvider。 Apparently the casting fails due to the different Class Loaders.显然,由于 Class 加载程序不同,铸造失败。 I suppose the casting fails in my web.xml file when I define the Authentication Filter:我想当我定义身份验证过滤器时,我的 web.xml 文件中的转换失败:

<!-- Container Based Authentication filter -->
  <filter>
    <filter-name>Container Based Authentication Filter</filter-name>
    <filter-class>org.camunda.bpm.webapp.impl.security.auth.ContainerBasedAuthenticationFilter</filter-class>
    <init-param>
      <param-name>authentication-provider</param-name>
      <param-value>com.somename.sec.sso.camunda.KeycloakSSOAuthenticationProvider</param-value>
    </init-param>
  </filter>

Is there a way to force KeycloakSSOAuthenticationProvider to be loaded with the same class loader as AuthenticationProvider ?有没有办法强制KeycloakSSOAuthenticationProvider加载与AuthenticationProvider相同的 class 加载程序? Or how should I approach this problem?或者我应该如何解决这个问题?

I was having similar casting errors with Keycloak.我在使用 Keycloak 时遇到了类似的铸造错误。 The issue for me was that part of the set up for running keycloak on Tomcat (not embedded tomcat) you have to copy a number of keycloak library files into the tomcat_home/lib directory.对我来说,问题是在 Tomcat(非嵌入式 tomcat)上运行 keycloak 设置的一部分,您必须将许多 keycloak 库文件复制到 tomcat_home/lib 目录中。 You then also need those libs available to your java code, so they were included in the POM file as well.然后,您还需要 java 代码可用的那些库,因此它们也包含在 POM 文件中。

The solution was to set the <scope>provided</scope> for the keycloak dependencies in the pom file.解决方案是为 pom 文件中的 keycloak 依赖项设置 <scope>provided</scope>。 That way the code would compile, but there wouldn't be two versions of the class on the classpath while running.这样代码可以编译,但运行时类路径上不会有两个版本的 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM