升级OpenLiberty至22.x后Ehcache与wsSecurity-1.1冲突问题
解决OpenLiberty wsSecurity-1.1与应用EhCache冲突问题
问题概述
基于Java 8/Java EE 8的遗留应用,升级OpenLiberty到25.0.0.2(及22.x以上版本)后,wsSecurity-1.1特性的EHCacheReplayCache报错:
"Error configuring EHCacheReplayCache: ()Service dependency declared by org.ehcache.core.spi.service.StatisticsService is not a Service: org.ehcache.core.spi.service.StatisticsService"
核心原因是应用打包的EhCache 3.10.8与wsSecurity自带的EhCache版本发生类加载冲突,导致无法找到正确的DefaultStatisticsServiceFactory实现类。
可行解决方案
方案1:配置类加载隔离,拆分双方的EhCache类加载路径
在server.xml中给应用配置类加载器,让wsSecurity依赖的EhCache核心服务类由Liberty父类加载器加载,应用自身的EhCache类由应用类加载器加载:
<application id="your-app" name="your-app" location="your-app.ear" type="ear"> <classloader delegation="parentLast"> <!-- 让wsSecurity需要的EhCache服务类走父类加载器 --> <package name="org.ehcache.core.spi.service.**" delegation="parentFirst" /> <package name="org.ehcache.core.internal.statistics.**" delegation="parentFirst" /> <!-- 应用其他EhCache类保持应用类加载器加载 --> <package name="org.ehcache.**" exclude="org.ehcache.core.spi.service.**|org.ehcache.core.internal.statistics.**" delegation="parentLast" /> </classloader> </application>
这种方式无需修改应用依赖,直接通过类加载规则隔离冲突类。
方案2:统一使用Liberty提供的EhCache,共享JCache实例
- 调整Maven依赖:将应用的EhCache相关依赖设置为
provided,避免打包进EAR:<dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.8.1</version> <!-- 与wsSecurity使用的版本保持一致 --> <scope>provided</scope> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache-jcache</artifactId> <version>3.8.1</version> <scope>provided</scope> </dependency> - 配置Liberty的JCache特性:在
server.xml中启用jcache-1.0,并指定EhCache提供者:<featureManager> <feature>wsSecurity-1.1</feature> <feature>jcache-1.0</feature> <!-- 保留应用所需的其他特性 --> </featureManager> <jcacheProvider id="ehcacheProvider" class="org.ehcache.jsr107.EhcacheCachingProvider" libraryRef="ehcacheLib"/> <library id="ehcacheLib"> <fileset dir="${server.config.dir}/lib" includes="ehcache-3.8.1.jar, ehcache-jcache-3.8.1.jar"/> </library> - 修改Hibernate配置:调整
persistence.xml,移除自定义的JCache提供者配置,让Hibernate使用Liberty全局的JCache:<properties> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/> <property name="hibernate.cache.region.factory_class" value="jcache"/> <property name="hibernate.javax.cache.missing_cache_strategy" value="create"/> <!-- 删除 hibernate.javax.cache.provider 和 hibernate.javax.cache.uri 配置 --> </properties> - 迁移缓存配置:将应用的
ehcache.xml复制到Liberty的server.config.dir/configDropins/defaults目录下,让全局JCache加载该配置。
方案3:替换wsSecurity的Nonce缓存实现
如果不想调整类加载或依赖,可以自定义Nonce缓存替代默认的EHCache实现:
- 实现
org.apache.wss4j.dom.cache.ReplayCache接口,比如做一个简单的内存缓存:public class CustomNonceCache implements ReplayCache { private final ConcurrentHashMap<String, Long> cache = new ConcurrentHashMap<>(); private static final long EXPIRY_MS = 5 * 60 * 1000; // 5分钟过期 @Override public void add(String nonce, String createdTime) throws WSSecurityException { cache.put(nonce, System.currentTimeMillis() + EXPIRY_MS); } @Override public boolean contains(String nonce, String createdTime) throws WSSecurityException { Long expiry = cache.get(nonce); if (expiry == null || System.currentTimeMillis() > expiry) { cache.remove(nonce); return false; } return true; } @Override public void clear() throws WSSecurityException { cache.clear(); } } - 在
server.xml中配置使用自定义缓存:<wsSecurity> <nonceCache className="com.your.package.CustomNonceCache" /> </wsSecurity>
验证步骤
- 重启OpenLiberty,检查启动日志是否无
EHCacheReplayCache相关错误; - 发送SOAP请求,确认请求正常返回,无SOAP错误;
- 验证Hibernate二级缓存:开启Hibernate缓存日志(
org.hibernate.cache设为DEBUG),检查查询是否命中缓存。
内容的提问来源于stack exchange,提问作者tormandob
相关产品推荐
相关产品推荐

