Can a spring ldap repository project access two different ldap directories? -
i trying create spring rest application return values may come 2 different ldap directory servers. possible using spring ldap repositories? possible create more 1 ldaptemplate , contextsource can query both directories?
you can configure separate ldaptemplate
, contextsource
beans each ldap directory.
you can refer following basic configuration (javaconfig);
@configuration @enableldaprepositories(basepackages = "com.foo.ldap1.repositories", ldaptemplateref="ldaptemplate1") public class ldap1configuration { @autowired environment env; @bean public ldapcontextsource contextsource1() { ldapcontextsource contextsource= new ldapcontextsource(); contextsource.seturl(env.getrequiredproperty("ldap1.url")); contextsource.setbase(env.getrequiredproperty("ldap1.base")); contextsource.setuserdn(env.getrequiredproperty("ldap1.user")); contextsource.setpassword(env.getrequiredproperty("ldap1.password")); return contextsource; } @bean(name="ldaptemplate1") public ldaptemplate ldaptemplate1() { return new ldaptemplate(contextsource1()); } }
@configuration @enableldaprepositories(basepackages = "com.foo.ldap2.repositories", ldaptemplateref="ldaptemplate2") public class ldap2configuration { @bean public ldapcontextsource contextsource2() { ldapcontextsource contextsource= new ldapcontextsource(); contextsource.seturl(env.getrequiredproperty("ldap2.url")); contextsource.setbase(env.getrequiredproperty("ldap2.base")); contextsource.setuserdn(env.getrequiredproperty("ldap2.user")); contextsource.setpassword(env.getrequiredproperty("ldap2.password")); return contextsource; } @bean(name="ldaptemplate2") public ldaptemplate ldaptemplate2() { return new ldaptemplate(contextsource2()); } }
then can refer each instance in application per following;
@autowired @qualifier("ldaptemplate1") private ldaptemplate ldaptemplate1;
@autowired @qualifier("ldaptemplate2") private ldaptemplate ldaptemplate2;
side note; if number of ldap directories increases better implement ldaptemplate
factory takes connection details , returns ldaptemplate
instances (example).
Comments
Post a Comment