You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pac4j实现JEE的OIDC授权码流遇类转换异常求助

问题:JEE环境下pac4j OIDC授权码流配置问题

背景

我需要在JEE环境中实现OIDC授权码流,获取并验证access token和id token用于Web应用后续请求,但找到的示例多基于Spring等框架。因必须使用javax而非Jakarta,无法采用pac4j 6.0版本,目前已完成初始重定向配置,但回调环节卡住,无法获取返回数据或存入令牌到会话。

Gradle依赖配置

dependencies {
    // https://mvnrepository.com/artifact/org.pac4j/jee-pac4j
    implementation 'org.pac4j:jee-pac4j:6.1.0'
    // https://mvnrepository.com/artifact/org.pac4j/pac4j-core
    implementation 'org.pac4j:pac4j-core:5.7.0'
    implementation 'org.pac4j:pac4j-oidc:5.7.0'
    implementation 'org.pac4j:pac4j-javaee:5.7.0'
...
}

出现的异常

当IDP返回认证结果时,抛出类转换异常:

08-Apr-2023 17:12:09.751 SEVERE [http-nio-8080-exec-8] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [default] in context with path [/Gradle___com_example___pac4j_1_0_SNAPSHOT_war] threw exception
    java.lang.ClassCastException: class `org.pac4j.jee.context.JEEContext` cannot be cast to class `org.pac4j.core.context.JEEContext` (`org.pac4j.jee.context.JEEContext` and `org.pac4j.core.context.JEEContext` are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @5ababd21)
        at org.pac4j.core.http.adapter.JEEHttpActionAdapter.adapt(JEEHttpActionAdapter.java:27)
        at org.pac4j.core.engine.DefaultCallbackLogic.perform(DefaultCallbackLogic.java:94)
        at com.example.sampleweb.pac4j.CustomCallbackFilter.internalFilter(CustomCallbackFilter.java:75)

异常指向CustomCallbackFilter.java中的代码行:

callbackLogic.perform(context, new JEESessionStoreFactory().newSessionStore(), getConfig(), JEEHttpActionAdapter.INSTANCE, "/", true, getDefaultClient());

我尝试切换不同包的JEEContext实现,仍无法解决org.pac4j.jee.context.JEEContext与org.pac4j.core.context.JEEContext的冲突问题。

请求帮助

请问是否有相关示例或单元测试可参考,以正确配置、组合和调用各组件?


内容的提问来源于stack exchange,提问作者Thorsten Stumeson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:13:24