1

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?

Saikat
  • 14,222
  • 20
  • 104
  • 125

1 Answers1

2

You can configure separate ldapTemplate and contextSource beans for each LDAP directory.

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;

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).

Community
  • 1
  • 1
Mert Z.
  • 553
  • 1
  • 8
  • 15
  • I had tried this but couldn't figure out how I would connect each ldapTemplate to an ldap repository. I am using the @EnableRepositories annotation on my LdapConfiguration class. I need to have different ldap repository classes for my different ldap directories. – robin karlin Jun 23 '16 at 14:33
  • You can set the `ldapTemplateRef` property of `@EnableLdapRepositories` in order to configure the name of the `ldaptemplate` bean to be used. In your case, you should configure separate configuration classes having separate @EnableLdapRepositories annotation for your LDAP directories. – Mert Z. Jun 24 '16 at 06:53