简体   繁体   中英

Spring LDAP with dynamic base DN

I have following LDAP scheme:

在此处输入图片说明

Every subtree contains organization unit Team . I want to find all Teams from specific subtree. To do that I use LdapTemplate class and findAll() methods.

ldapTemplate.findAll(Team.class);

When I set base in LdapContextSource to dc=global,dc=id,dc=pl it returns me Teams from global subtree. When I change base to dc=id,dc=pl it returns me Teams from all subtrees.

The problem is that I want to use dynamic base, to find Teams from specific subtree. I've tried multiple methods to achieve that, but none of them gives me results.

Method 1: find

Name nameBase = LdapUtils.newLdapName("dc=global");
return ldapTemplate.find(query().base(nameBase).where("ou").is("team"), Team.class);

returns empty list

Method 2: findAll

Name nameBase = LdapUtils.newLdapName("dc=global");
SearchControls searchControls = new SearchControls();
return ldapTemplate.findAll(nameBase, searchControls, Team.class);

returns empty list

At first It looks like working correctly, because when I change subtree name to some which not exists I get javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]

Any ideas why I get proper results in this code:

LdapContextSource contextSource = new LdapContextSource();
contextSource.setBase("dc=global,dc=id,dc=pl");

LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
return ldapTemplate.findAll(Team.class);

And empty list from this one:

LdapContextSource contextSource = new LdapContextSource();
contextSource.setBase("dc=id,dc=pl");

LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
Name nameBase = LdapUtils.newLdapName("dc=global");
SearchControls searchControls = new SearchControls();
return ldapTemplate.findAll(nameBase, searchControls, Team.class);

I use Spring-ldap-core 2.0.3

I've found solution.

First

Adding proper scope to SearchControls

SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
return ldapTemplate.findAll(base, searchControls, Team.class);

Second

Changing query parameters to check if cn is present

return ldapTemplate.find(query().base(base).where("cn").isPresent(), Team.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM