2017-07-26 11 views
0

Spring LDAPを使用して複数のLDAPリポジトリを設定したいとします。私の目的は、すべてのリポジトリ内のオブジェクトを同時に作成または更新することです。複数のLDAPリポジトリ(Spring 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()); 
    } 
} 

かつ完全であることを、1つのリポジトリ:

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

あなたは上記LdapConfigurationを設定するためにapplication.propertiesspring.ldap接頭辞を使用することができます。利用可能なプロパティは、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); 
    } 
} 

LdapConfiguration2application.propertiesspring.ldap2接頭辞によって設定されます。

2)私はリポジトリを拡張することが解決策だとは思わない。私は@Serviceメソッドを作成してリポジトリを反復し、更新を適用することをお勧めします。私は以下の2つのアプローチを提供します。

例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(私が考える最初のインスタンス)を1つだけ取得します。 –

+0

この例は、複数のLdapRepositoryを実装しているかどうかによって異なります。 – ryan2049

+0

2つのエンティティで完全な例を使って答えを追加しました –

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. 第2の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。

関連する問題