spring-ldap
spring-ldap copied to clipboard
use multiple ldaptemplates with different ldap directories
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.
This site is in the top 10 results shown by Google. Can we provide a solution here or at least link to one ?
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.
I am not sure what you mean or how this could look like. Can you share a code snippet ?
Use method injection with:
@Autowired
public void setContextSources(List<ContextSource> sources) {
for (ContextSource source : sources)
this.ldapTemplates.add(new LdapTemplate(source));
}
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
This happens in your DAO/Repository/Service. The context sources are created in your beans.xml or in App config via Java.
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
}