2017-07-26 133 views
0

我想用Spring LDAP設置多個LDAP存儲庫。我的目標是同時創建或更新所有存儲庫中的對象。使用Spring LDAP存儲庫的多個LDAP存儲庫

我使用LdapRepository Spring接口,我認爲現在是不可能的。

我在想我是否可以創建自己的LdapRepository來擴展Spring,但我不知道如何開始。

這是我的配置:

@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()); 
    } 
} 

而且是完整的,一個倉庫:

public interface LdapUserRepository extends LdapRepository<LdapUser> { 

} 

任何想法,該怎麼辦呢?

在此先感謝您的幫助。

回答

1

1)可以指定多個LDAP存儲庫配置。請看下面的例子。 [注意:這取決於春季啓動庫]

@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); 
    } 
} 

可以使用spring.ldap前綴application.properties配置上述LdapConfiguration。您可以通過檢出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將由spring.ldap2前綴application.properties進行配置。

2)我不認爲擴展存儲庫是解決方案。我建議創建一個@Service方法遍歷您的存儲庫並應用更新。我將在下面提供兩種方法。

例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 
    } 
} 

我還沒有編譯的代碼,所以我們還是我知道如果事情是關閉的,如果你需要更多的指導。

+0

在示例2中,我只獲得一個LdapRepository(第一個實例化我認爲)。 –

+0

這個例子取決於你有多個LdapRepository的實現。 – ryan2049

+0

我用兩個實體的完整示例添加了答案 –

0

,如果我理解正確的,但在這裏我不知道的是我們所做的事情:

  1. 全球配置類

    @Bean("odm") 
    public ObjectDirectoryMapper odm() { 
        return new DefaultObjectDirectoryMapper(); 
    }; 
    
  2. 首先LDAP配置類

    @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. 第二個LDAP配置類

    @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庫

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

    public class LdapFamilyRepository extends SimpleLdapRepository<LdapFamily> { 
    
        public LdapFamilyRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm) { 
         super(ldapOperations, odm, LdapFamily.class); 
        } 
    } 
    
  6. LdapUser服務(同樣爲LdapFamily服務)

    @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); 
        } 
    } 
    

    }

我們刪除LDAP回購的自動配置:

@EnableLdapRepositories(basePackages = "com.afklm.paul.repository.ldap", ldapTemplateRef = "ldapTwoTemplate") 

感謝ryan2049您的幫助。