Spring ldap存储库项目可以访问两个不同的ldap目录吗? [英] Can a spring ldap repository project access two different ldap directories?
问题描述
我正在尝试创建一个spring rest应用程序,以返回可能来自两个不同ldap目录服务器的值.使用Spring ldap存储库是否有可能?是否可以创建多个ldaptemplate和contextsource以便我可以查询两个目录?
I am trying to create a spring rest application to return values that may come from two different ldap directory servers. Is this possible using spring ldap repositories? Is it possible to create more than one ldaptemplate and contextsource so I can query both directories?
推荐答案
您可以为每个LDAP目录配置单独的ldapTemplate
和contextSource
Bean.
You can configure separate ldapTemplate
and contextSource
beans for each LDAP directory.
您可以参考以下基本配置(JavaConfig);
You can refer to the following basic configuration (JavaConfig);
@Configuration
@EnableLdapRepositories(basePackages = "com.foo.ldap1.repositories", ldapTemplateRef="ldapTemplate1")
public class Ldap1Configuration {
@Autowired
Environment env;
@Bean
public LdapContextSource contextSource1() {
LdapContextSource contextSource= new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap1.url"));
contextSource.setBase(env.getRequiredProperty("ldap1.base"));
contextSource.setUserDn(env.getRequiredProperty("ldap1.user"));
contextSource.setPassword(env.getRequiredProperty("ldap1.password"));
return contextSource;
}
@Bean(name="ldapTemplate1")
public LdapTemplate ldapTemplate1() {
return new LdapTemplate(contextSource1());
}
}
@Configuration
@EnableLdapRepositories(basePackages = "com.foo.ldap2.repositories", ldapTemplateRef="ldapTemplate2")
public class Ldap2Configuration {
@Bean
public LdapContextSource contextSource2() {
LdapContextSource contextSource= new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap2.url"));
contextSource.setBase(env.getRequiredProperty("ldap2.base"));
contextSource.setUserDn(env.getRequiredProperty("ldap2.user"));
contextSource.setPassword(env.getRequiredProperty("ldap2.password"));
return contextSource;
}
@Bean(name="ldapTemplate2")
public LdapTemplate ldapTemplate2() {
return new LdapTemplate(contextSource2());
}
}
然后,您可以按照以下说明引用应用程序中的每个实例;
Then you can refer to each instance in your application as per following;
@Autowired
@Qualifier("ldapTemplate1")
private LdapTemplate ldapTemplate1;
@Autowired
@Qualifier("ldapTemplate2")
private LdapTemplate ldapTemplate2;
旁注;如果LDAP目录数量增加,则最好实现一个ldaptemplate
工厂,该工厂具有连接详细信息并返回ldaptemplate
实例(例如).
Side note; If the number of LDAP directories increases then it will be better to implement a ldaptemplate
factory which takes connection details and returns ldaptemplate
instances (example).
这篇关于Spring ldap存储库项目可以访问两个不同的ldap目录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!