2015-09-04 57 views
0

我有以下LDAP方案:春LDAP动态基础DN

enter image description here

每个子树包含组织单位。我想从特定的子树中找到所有团队。要做到这一点,我使用LdapTemplate类和findAll()方法。

ldapTemplate.findAll(Team.class); 

当我设置在LdapContextSource基地dc=global,dc=id,dc=pl从全球树返回我的团队。当我将基数改为dc=id,dc=pl时,它会返回来自所有子树的团队。

问题是我想使用动态基础,从特定的子树中查找团队。我尝试了多种方法来实现这一点,但没有一个给我的结果。

方法1:找到

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

回报空单

方法2:的findAll

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

回报空单

起初,它看起来像正常工作,因为当我改变子树名其中一些不存在的,我得到javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]

任何想法,为什么我得到这个代码正确的结果:

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

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

而空从这一个列表:

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

我使用Spring-LDAP的核心2.0.3

回答

0

我找到了解决方案。

首先

添加适当的范围,以SearchControls

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

更改查询参数,以检查是否CN存在

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