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

使用Spring Security实现LDAP认证时Gradle依赖异常求助

排查Spring LDAP与Spring Security LDAP依赖导致的ClassNotFoundException问题

你遇到的这两个ClassNotFoundException确实大概率是版本不兼容导致的,下面我来帮你一步步分析和解决:


核心问题:版本兼容性不匹配

你当前配置的依赖组合存在明显的版本冲突风险:

  • spring-ldap-core:2.3.1.RELEASE 属于Spring LDAP 2.x系列,它依赖的是Spring Framework 4.3+版本
  • spring-security-ldap:4.0.4.RELEASE 属于Spring Security 4.0.x系列,这个版本默认依赖的是Spring LDAP 1.x系列(而非2.x)

两个系列的包结构、类实现有较大差异,同时依赖的Spring核心库版本也不一致,这会直接导致类加载时找不到对应的目标类(比如ContextSource或LdapUserSearch)。


解决方案:调整为兼容的版本组合

你可以选择以下两种方案之一来解决问题:

方案1:降级Spring LDAP到与Spring Security 4.0.x兼容的版本

把Spring LDAP的版本改为1.3.x系列的稳定版,匹配Spring Security 4.0.4的依赖要求:

compile group: 'org.springframework.ldap', name: 'spring-ldap-core', version: '1.3.2.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-ldap', version: '4.0.4.RELEASE'

方案2:升级Spring Security到支持Spring LDAP 2.x的版本

Spring Security 4.2.x及以上版本开始支持Spring LDAP 2.x,你可以升级Spring Security到同系列的最新稳定版,保留你原本想用的Spring LDAP 2.3.1:

compile group: 'org.springframework.ldap', name: 'spring-ldap-core', version: '2.3.1.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-ldap', version: '4.2.13.RELEASE'

额外排查步骤:确认依赖树无隐藏冲突

为了彻底确认没有其他依赖冲突,你可以运行Gradle的依赖分析命令,查看当前项目的完整依赖树:

./gradlew dependencies

重点查看compileClasspath下的org.springframework.ldap和org.springframework.security相关依赖,检查是否存在多个版本的依赖被引入,或者某个依赖被意外排除的情况。如果发现冲突,可以通过exclude规则手动排除不兼容的版本。


内容的提问来源于stack exchange,提问作者Jayesh Mulwani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:38:44