具有Spring LDAP存储库的多个LDAP存储库 [英] Multiple LDAP repositories with Spring LDAP Repository
问题描述
我想用Spring LDAP设置多个LDAP存储库.我的目的是同时在所有存储库中创建或更新对象.
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.
我使用LdapRepository Spring界面,我认为目前尚不可能.
I use LdapRepository Spring interface and I think that isn't possible for now.
我想知道是否可以创建自己的LdapRepository来扩展Spring,但是我不知道如何开始.
I wonder if I can create my own LdapRepository extending the Spring one but I have no idea how to start.
这是我的配置:
@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> {
}
有什么想法吗?
在此先感谢您的帮助.
推荐答案
1)可以指定多个LDAP存储库配置.请参见以下示例. [注意:这取决于spring-boot库]
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);
}
}
您可以使用application.properties
中的spring.ldap
前缀来配置上述LdapConfiguration
.您可以通过检出
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
将由application.properties
中的spring.ldap2
前缀配置.
2)我不认为扩展存储库是解决方案.我建议创建一个遍历存储库并应用更新的@Service
方法.我将在下面提供两种方法.
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.
示例1)
@Service
public class UpdateRepositories {
public void updateAllRepositories(LdapUserRepository userRepository1, LdapUserRepository userRepository2) {
// apply updates to userRepository1 and userRepository2
}
}
示例2)
@Service
public class UpdateRepositories {
public void updateAllRepositories(ApplicationContext appContext) {
Map<String, LdapRepository> ldapRepositories = appContext.getBeansofType(LdapRepository.class)
// iterate through map and apply updates
}
}
我尚未编译此代码,所以请告诉我是否有问题或需要其他指导.
这篇关于具有Spring LDAP存储库的多个LDAP存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!