2015-09-04 86 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);