Keycloak对接Infinispan集群出现EncodingException问题求助
问题原因与解决方案
核心原因
- 编码配置不匹配:你的Keycloak远程存储配置中未显式指定编码格式,客户端默认使用
application/x-protostream,而Infinispan服务器端配置的是application/x-jboss-marshalling,两端编码不一致,且Infinispan 14.x默认不提供这两种编码的转码器。 - JBoss Marshalling兼容性变化:Infinispan 14.x已将JBoss Marshalling标记为废弃,默认转码器集合中移除了
protostream <-> jboss-marshalling的转换器,即使两端配置了相同编码,若配置未生效也会触发错误。 - Marshaller与编码未同步:虽然指定了
KeycloakHotRodMarshallerFactory,但未同步配置编码,客户端仍会使用默认的protostream编码,与服务器端产生冲突。
解决步骤
显式配置Keycloak远程存储的编码
在你的<remote-store>节点下添加<encoding>配置,强制指定键和值的编码为application/x-jboss-marshalling:<distributed-cache name="loginFailures" owners="2"> <expiration lifespan="900000000000000000"/> <remote-store cache="loginFailures" xmlns="urn:infinispan:config:store:remote:13.0" purge="false" preload="false" shared="true" segmented="false" connect-timeout="${env.KEYCLOAK_REMOTE_ISPN_CONN_TIMEOUT:2000}"> <remote-server host="${env.INFINISPAN_SERVER}" port="${infinispan.bind.port:11222}"/> <security> <authentication> <digest username="${env.KEYCLOAK_REMOTE_ISPN_USERNAME:keycloak}" password="${env.KEYCLOAK_REMOTE_ISPN_PASSWORD:password}" realm="default"/> </authentication> </security> <!-- 添加编码配置 --> <encoding key="application/x-jboss-marshalling" value="application/x-jboss-marshalling"/> <property name="rawValues">true</property> <property name="marshaller">org.keycloak.cluster.infinispan.KeycloakHotRodMarshallerFactory</property> </remote-store> </distributed-cache>确认Infinispan服务器端缓存编码
确保Infinispan服务器的loginFailures缓存也配置了相同编码,在服务器配置文件中添加:<distributed-cache name="loginFailures" owners="2"> <encoding key="application/x-jboss-marshalling" value="application/x-jboss-marshalling"/> <expiration lifespan="900000000000000000"/> </distributed-cache>添加JBoss Marshalling转码器依赖
若上述配置后仍报错,需为Infinispan服务器添加转码器依赖。对于容器化部署,可自定义镜像并添加以下依赖:<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-jboss-marshalling</artifactId> <version>14.0.0.Final</version> </dependency>验证版本兼容性
确认Keycloak 21.0.2使用的Infinispan客户端版本与服务器14.0.x匹配。Keycloak 21默认适配Infinispan 14.x,若存在版本差异,需调整客户端或服务器版本。
内容的提问来源于stack exchange,提问作者Neelesh Gurjar
相关产品推荐
相关产品推荐

