Multiple LDAP repositories with Spring LDAP Repository -
i set more 1 ldap repositories spring ldap. aim create or update objects in repositories @ same time.
i use ldaprepository spring interface , think isn't possible now.
i wonder if can create own ldaprepository extending spring 1 have no idea how start.
this 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 complete, 1 repository:
public interface ldapuserrepository extends ldaprepository<ldapuser> { }
any idea how ?
thanks in advance help.
1) possible specify more 1 ldap repository configuration. please see following example. [notice: 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 spring.ldap
prefix in application.properties
configure above ldapconfiguration
. can see available properties 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
configured spring.ldap2
prefix in application.properties
.
2) don't think extending repository solution. recommend creating @service
method iterated through repositories , applied updates. provide 2 approaches below.
example 1)
@service public class updaterepositories { public void updateallrepositories(ldapuserrepository userrepository1, ldapuserrepository userrepository2) { // apply updates userrepository1 , userrepository2 } }
example 2)
@service public class updaterepositories { public void updateallrepositories(applicationcontext appcontext) { map<string, ldaprepository> ldaprepositories = appcontext.getbeansoftype(ldaprepository.class) // iterate through map , apply updates } }
i haven't compiled code, let me know if off or if need additional guidance.
Comments
Post a Comment