如何为Spring Data LDAP Repository指定特定LDAP连接(LDAPTemplate)
这个问题确实在官方文档里提得比较隐晦,不过咱们可以通过多组@EnableLdapRepositories配置+自定义LdapTemplate实例来解决,核心思路是把不同的Repository和对应的LDAP连接上下文绑定起来,具体步骤如下:
1. 配置多个LDAP上下文源(ContextSource)
每个LDAP连接都需要一个独立的ContextSource实例,用来配置连接URL、认证信息等:
@Configuration public class MultipleLdapConfig { // 第一个LDAP连接的上下文源 @Bean(name = "ldapContextSource1") public DefaultSpringSecurityContextSource ldapContextSource1() { DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://first-ldap-server:389/dc=example,dc=com"); contextSource.setUserDn("cn=admin,dc=example,dc=com"); contextSource.setPassword("admin123"); contextSource.afterPropertiesSet(); return contextSource; } // 第二个LDAP连接的上下文源 @Bean(name = "ldapContextSource2") public DefaultSpringSecurityContextSource ldapContextSource2() { DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://second-ldap-server:389/dc=test,dc=org"); contextSource.setUserDn("cn=admin,dc=test,dc=org"); contextSource.setPassword("test456"); contextSource.afterPropertiesSet(); return contextSource; } }
2. 为每个上下文源创建对应的LdapTemplate
每个LdapTemplate绑定一个ContextSource,后续Repository会直接使用这些模板:
@Configuration public class MultipleLdapTemplateConfig { @Bean(name = "ldapTemplate1") public LdapTemplate ldapTemplate1(@Qualifier("ldapContextSource1") ContextSource contextSource) { return new LdapTemplate(contextSource); } @Bean(name = "ldapTemplate2") public LdapTemplate ldapTemplate2(@Qualifier("ldapContextSource2") ContextSource contextSource) { return new LdapTemplate(contextSource); } }
3. 分组Repository并绑定对应的LdapTemplate
这里有两种常用方式,选一种适合你的即可:
方式一:按包路径分组(推荐,简单直观)
把需要关联不同LDAP连接的Repository放在不同的包下,比如:
- 关联第一个LDAP的Repository放在
x.y.z.repository.ldap1 - 关联第二个LDAP的Repository放在
x.y.z.repository.ldap2
然后分别用@EnableLdapRepositories注解配置每组,指定对应的ldapTemplateRef:
// 配置第一组Repository,绑定ldapTemplate1 @Configuration @EnableLdapRepositories( basePackages = "x.y.z.repository.ldap1", ldapTemplateRef = "ldapTemplate1", repositoryImplementationPostfix = "Impl1" // 避免实现类重名冲突 ) public class Ldap1RepositoryConfig { } // 配置第二组Repository,绑定ldapTemplate2 @Configuration @EnableLdapRepositories( basePackages = "x.y.z.repository.ldap2", ldapTemplateRef = "ldapTemplate2", repositoryImplementationPostfix = "Impl2" ) public class Ldap2RepositoryConfig { }
这样你的UserRepository如果放在x.y.z.repository.ldap1下,就会自动使用ldapTemplate1对应的LDAP连接了。
方式二:用自定义注解筛选Repository(适合不想分包的场景)
如果不想调整包结构,可以给Repository标记自定义注解,比如:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Ldap1Repository { }
然后给你的UserRepository加上这个注解:
@Repository @Ldap1Repository public interface UserRepository extends LdapRepository<User> { User findByUserId(String userId); }
接着在配置类里用includeFilters指定筛选规则:
@Configuration @EnableLdapRepositories( basePackages = "x.y.z.repository", includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Ldap1Repository.class), ldapTemplateRef = "ldapTemplate1", repositoryImplementationPostfix = "Impl1" ) public class Ldap1RepositoryConfig { } // 同理配置第二组,用另一个自定义注解比如@Ldap2Repository
关键注意点
- 每个
@EnableLdapRepositories必须指定唯一的repositoryImplementationPostfix,否则Spring生成的Repository实现类会重名导致冲突。 - 如果你的Repository有自定义实现类,也要对应调整后缀,比如
UserRepositoryImpl1对应第一组的实现。
内容的提问来源于stack exchange,提问作者Gábor Lipták
相关产品推荐
相关产品推荐

