问题
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.
回答1:
1) It is possible specify more than one LDAP Repository configuration. Please see the following example. [Notice: This depends on spring-boot libraries]
@Configuration
@EnableLdapRepositories("com.xxx.repository.ldap")
@EnableConfigurationProperties(LdapProperties.class)
public class LdapConfiguration {
@Autowired
private Environment environment;
@Bean(name="contextSource1")
public LdapContextSource contextSourceTarget(LdapProperties ldapProperties) {
LdapContextSource source = new LdapContextSource();
source.setUserDn(this.properties.getUsername());
source.setPassword(this.properties.getPassword());
source.setBase(this.properties.getBase());
source.setUrls(this.properties.determineUrls(this.environment));
source.setBaseEnvironmentProperties(Collections.<String,Object>unmodifiableMap(this.properties.getBaseEnvironment()));
return source;
}
@Bean
public LdapTemplate ldapTemplate(@Qualifier("contextSource1") LdapContextSource contextSource){
return new LdapTemplate(contextSource);
}
}
You can use the spring.ldap
prefix in application.properties
to configure the above LdapConfiguration
. You can see the available properties by checking out https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java.
@Configuration
@EnableLdapRepositories(basePackages="com.yyy.repository.ldap", ldapTemplateRef="ldapTemplate2")
public class LdapConfiguration2 {
@Autowired
private Environment environment;
@Bean(name="ldapProperties2")
@ConfigurationProperties(prefix="spring.ldap2")
public LdapProperties ldapProperties() {
return new LdapProperties();
}
@Bean(name="contextSource2")
public LdapContextSource contextSourceTarget(@Qualifier("ldapProperties2") LdapProperties ldapProperties) {
LdapContextSource source = new LdapContextSource();
source.setUserDn(this.properties.getUsername());
source.setPassword(this.properties.getPassword());
source.setBase(this.properties.getBase());
source.setUrls(this.properties.determineUrls(this.environment));
source.setBaseEnvironmentProperties(Collections.<String,Object>unmodifiableMap(this.properties.getBaseEnvironment()));
return source;
}
@Bean(name="ldapTemplate2")
public LdapTemplate ldapTemplate(@Qualifier("contextSource2") LdapContextSource contextSource){
return new LdapTemplate(contextSource);
}
}
LdapConfiguration2
will be configured by the spring.ldap2
prefix in application.properties
.
2) I don't think extending the Repository is the solution. I would recommend creating a @Service
method that iterated through your repositories and applied the updates. I will provide two approaches below.
Example 1)
@Service
public class UpdateRepositories {
public void updateAllRepositories(LdapUserRepository userRepository1, LdapUserRepository userRepository2) {
// apply updates to userRepository1 and userRepository2
}
}
Example 2)
@Service
public class UpdateRepositories {
public void updateAllRepositories(ApplicationContext appContext) {
Map<String, LdapRepository> ldapRepositories = appContext.getBeansofType(LdapRepository.class)
// iterate through map and apply updates
}
}
I haven't compiled this code, so let me know if something is off or if you need additional guidance.
回答2:
I don't known if I understood correctly but here is what we did:
Global configuration class
@Bean("odm") public ObjectDirectoryMapper odm() { return new DefaultObjectDirectoryMapper(); };
First LDAP configuration class
@Configuration @PropertySource("classpath:ldap-one.properties") public class LdapOneConfiguration { @Autowired Environment ldapProperties; @Bean(name = "contextSourceOne") public LdapContextSourceCustom contextSourceLdapOneTarget() { LdapContextSourceCustom ldapContextSource = new LdapContextSourceCustom(); ldapContextSource.setUrl(ldapProperties.getProperty("ldap-one.url")); ldapContextSource.setBase(ldapProperties.getProperty("ldap-one.base")); ldapContextSource.setUserDn(ldapProperties.getProperty("ldap-one.userDn")); ldapContextSource.setPassword(ldapProperties.getProperty("ldap-one.password")); ldapContextSource.setKeyStoreFile(ldapProperties.getProperty("ldap-one.truststore")); return ldapContextSource; } @Bean(name = "ldapTemplateOne") public LdapTemplate ldapOneTemplate(@Qualifier("contextSourceOne") LdapContextSourceCustom contextSource) { return new LdapTemplate(contextSource); } @Bean(name = "ldapUserRepoOne") public LdapUserRepository ldapUserRepositoryOne(@Qualifier("ldapTemplateOne") LdapTemplate ldapTemplate, @Qualifier("odm") ObjectDirectoryMapper odm) { return new LdapUserRepository(ldapTemplate, odm); } @Bean(name = "ldapFamilyRepoOne") public LdapFamilyRepository ldapFamilyRepositoryOne(@Qualifier("ldapTemplateOne") LdapTemplate ldapTemplate, @Qualifier("odm") ObjectDirectoryMapper odm) { return new LdapFamilyRepository(ldapTemplate, odm); } }
Second LDAP configuration class
@Configuration @PropertySource("classpath:ldap-two.properties") public class LdapTwoConfiguration { @Autowired Environment ldapProperties; @Bean(name = "contextSourceTwo") public LdapContextSourceCustom contextSourceLdapTwoTarget() { LdapContextSourceCustom ldapContextSource = new LdapContextSourceCustom(); ldapContextSource.setUrl(ldapProperties.getProperty("ldap-two.url")); ldapContextSource.setBase(ldapProperties.getProperty("ldap-two.base")); ldapContextSource.setUserDn(ldapProperties.getProperty("ldap-two.userDn")); ldapContextSource.setPassword(ldapProperties.getProperty("ldap-two.password")); ldapContextSource.setKeyStoreFile(ldapProperties.getProperty("ldap-two.truststore")); return ldapContextSource; } @Bean(name = "ldapTemplateTwo") public LdapTemplate ldapTwoTemplate(@Qualifier("contextSourceTwo") LdapContextSourceCustom contextSource) { return new LdapTemplate(contextSource); } @Bean(name = "ldapUserRepoTwo") public LdapUserRepository ldapUserRepositoryTwo(@Qualifier("ldapTemplateTwo") LdapTemplate ldapTemplate, @Qualifier("odm") ObjectDirectoryMapper odm) { return new LdapUserRepository(ldapTemplate, odm); } @Bean(name = "ldapFamilyRepoTwo") public LdapFamilyRepository ldapFamilyRepositoryTwo(@Qualifier("ldapTemplateTwo") LdapTemplate ldapTemplate, @Qualifier("odm") ObjectDirectoryMapper odm) { return new LdapFamilyRepository(ldapTemplate, odm); } }
LdapUser repository
public class LdapUserRepository extends SimpleLdapRepository<LdapUser> { public LdapUserRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm) { super(ldapOperations, odm, LdapUser.class); } }
LdapFamily repository
public class LdapFamilyRepository extends SimpleLdapRepository<LdapFamily> { public LdapFamilyRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm) { super(ldapOperations, odm, LdapFamily.class); } }
LdapUser service (same for LdapFamily service)
@Service public class LdapUserServiceImpl implements LdapUserService { @Autowired private ApplicationContext appContext; private LdapUserRepository uniqueLdapUserRepo; private List<LdapUserRepository> ldapUserRepoList; @PostConstruct private void setUniqueRepo() { uniqueLdapUserRepo = appContext.getBeansOfType(LdapUserRepository.class).values().iterator().next(); ldapUserRepoList = new ArrayList<>(appContext.getBeansOfType(LdapUserRepository.class).values()); } @Override public LdapUser getUser(String uid) { return uniqueLdapUserRepo.findOne(query().where("uid").is(uid)); } @Override public void saveUser(LdapUser user) { for(LdapUserRepository repo: ldapUserRepoList){ repo.save(user); } }
}
We deleted the auto configuration of LDAP repo:
@EnableLdapRepositories(basePackages = "com.afklm.paul.repository.ldap", ldapTemplateRef = "ldapTwoTemplate")
Thanks ryan2049 for your help.
回答3:
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
来源:https://stackoverflow.com/questions/45319900/multiple-ldap-repositories-with-spring-ldap-repository