spring-ldap icon indicating copy to clipboard operation
spring-ldap copied to clipboard

use multiple ldaptemplates with different ldap directories

Open rfkarlin opened this issue 9 years ago • 7 comments
trafficstars

It is possible to use multiple databases with spring data with jpa. I would like the same ability with spring ldap (ldaptemplate) and multiple ldap directories.

rfkarlin avatar Jun 17 '16 16:06 rfkarlin

This site is in the top 10 results shown by Google. Can we provide a solution here or at least link to one ?

mar1ged avatar Sep 27 '18 11:09 mar1ged

Why don't you have a list injected and iterate over? This is what I did for at least a year and it served me well.

michael-o avatar Sep 27 '18 17:09 michael-o

I am not sure what you mean or how this could look like. Can you share a code snippet ?

mar1ged avatar Sep 27 '18 18:09 mar1ged

Use method injection with:

@Autowired
public void setContextSources(List<ContextSource> sources) {
  for (ContextSource source : sources)
    this.ldapTemplates.add(new LdapTemplate(source));
}

michael-o avatar Sep 27 '18 19:09 michael-o

Perhaps I didn't yet wrap my head around this, but where should I wire this in and where should I provide the credentials and the other stuff for the context sources ?

If you want to have a look I described more of what I am searching for in this SO post: https://stackoverflow.com/questions/52521083/configure-a-second-independent-ldaptemplate-for-spring-boot

mar1ged avatar Sep 27 '18 21:09 mar1ged

This happens in your DAO/Repository/Service. The context sources are created in your beans.xml or in App config via Java.

michael-o avatar Sep 28 '18 10:09 michael-o

When i call this methd in a class like this it's ldapTemplate has value but findAllByName() return 0!!!!If i remove the first ldapConfigUration and it's ldapTemplate,the findAllByName() for User work well!! please help me!Thanks!

//first ldapConfig
@Configuration
public class LdapConfiguration  {

    @Value("${spring.ldap.urls}")
    private String url;

    @Value("${spring.ldap.username}")
    private String userName;

    @Value("${spring.ldap.base}")
    private String base;

    @Value("${spring.ldap.password}")
    private String password;

    @Bean
    public LdapContextSource contextSource(){
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setBase(base);
        contextSource.setPassword(password);
        contextSource.setUrl(url);
        contextSource.setUserDn(userName);
        return contextSource;
    }

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


//second ldapConfiguration
@Configuration
@EnableLdapRepositories(basePackages = "com.example.batch.repository", ldapTemplateRef="userLdapTemplate")
public class SecondLdapConfiguration  {

    @Value("${spring.ldap.urls}")
    private String url;

    @Value("${spring.ldap.username}")
    private String userName;

    @Value("${spring.ldap.base.user}")
    private String base;

    @Value("${spring.ldap.password}")
    private String password;

    @Bean
    public LdapContextSource contextSource(){
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setBase(base);
        contextSource.setPassword(password);
        contextSource.setUrl(url);
        contextSource.setUserDn(userName);
        return contextSource;
    }

    @Bean("userLdapTemplate")
    public LdapTemplate userLdapTemplate(LdapContextSource ldapContextSource){
        return new LdapTemplate(ldapContextSource);
    }


//UserLdapRepository
@Repository
public interface UserLdapRepository extends LdapRepository<User> {

    List<User> findAllByName(String name);
}
//User Object class
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entry(objectClasses = {"person","top","dcObject","organization"},base = "dc=users")
public final class  User {

    @Id
    private Name dn;

    @Attribute(name = "cn")
    private String name;

........some details

}

soheilqalamkari avatar Mar 11 '20 13:03 soheilqalamkari