Multiple LDAP repositories with Spring LDAP Repository

自作多情 提交于 2019-12-03 20:47:12

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.

I don't known if I understood correctly but here is what we did:

  1. Global configuration class

    @Bean("odm")
    public ObjectDirectoryMapper odm() {
        return new DefaultObjectDirectoryMapper();
    };
    
  2. 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);
        }
    }
    
  3. 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);
        }
    
    }
    
  4. LdapUser repository

    public class LdapUserRepository extends SimpleLdapRepository<LdapUser> {
    
        public LdapUserRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm) {
            super(ldapOperations, odm, LdapUser.class);
        }
    }
    
  5. LdapFamily repository

    public class LdapFamilyRepository extends SimpleLdapRepository<LdapFamily> {
    
        public LdapFamilyRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm) {
            super(ldapOperations, odm, LdapFamily.class);
        }
    }
    
  6. 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.

Kevin T.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!