Multiple LDAP repositories with Spring LDAP Repository

Viewed 5792

I would like to set more than one LDAP repositories with Spring LDAP. My aim is to create or update objects in all repositories at the same time.

I use LdapRepository Spring interface and I think that isn't possible for now.

I wonder if I can create my own LdapRepository extending the Spring one but I have no idea how to start.

This my configuration :

@Configuration
@EnableLdapRepositories("com.xxx.repository.ldap")
@PropertySource("classpath:ldap.properties")
public class LdapConfiguration {

    @Autowired
    Environment ldapProperties;

    @Bean
    public LdapContextSourceCustom contextSourceTarget() {
        LdapContextSourceCustom ldapContextSource = new LdapContextSourceCustom();
        ldapContextSource.setUrl(ldapProperties.getProperty("ldap.url"));
        ldapContextSource.setBase(ldapProperties.getProperty("ldap.base"));
        ldapContextSource.setUserDn(ldapProperties.getProperty("ldap.userDn"));
        ldapContextSource.setPassword(ldapProperties.getProperty("ldap.password"));
        ldapContextSource.setKeyStoreFile(ldapProperties.getProperty("ldap.truststore"));

        return ldapContextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate(){
        return new LdapTemplate(contextSourceTarget());
    }
}

And to be complete, one repository:

public interface LdapUserRepository extends LdapRepository<LdapUser> {

}

Any idea how to do it ?

Thanks in advance for any help.

3 Answers

there is actually an easier way now:

create multiple configuration that is anotated with @EnableLdapRepositories with corresponding attributes

Create first configuration

@Configuration
@EnableLdapRepositories(basePackages = "first.ldap.package.repository.**", ldapTemplateRef = "firstLdapTemplate")
public class FirstLDAPConfig {
   ....detail


    @Bean("firstLdapTemplate")
    public LdapTemplate firstLdapTemplate() {
        ...template creation
    }

}

Create second configuration

@Configuration
@EnableLdapRepositories(basePackages = "second.ldap.package.repository.**", ldapTemplateRef = "secondLdapTemplate")
public class SecondLDAPConfig {
   ....detail

    @Bean("secondLdapTemplate")
    public LdapTemplate secondLdapTemplate() {
        ...template creation
    }
}

each configuration should handle it's own contextSource then only the specified repository within the EnableLdapRepositories annotation will use that specific ContextSource and LdapTemplate

Related