2011-11-08 66 views
0

我想通過使用Id從LDAP Active Directory中獲取一些用戶信息。 這裏是我試圖連接並獲取它的代碼。使用ID從LDAP活動目錄獲取數據?

SearchControls ctls = new SearchControls(); 
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
NamingEnumeration results = ctx.search("DC=erieinsurance,DC=com", "(&(objectCategory=user)(name{0}))", 
        new Object[]{Id}, // filter arguments 
       ctls); // search controls 
      } 
if (results.hasMoreElements()) { 

} 

它沒有返回給定名稱和sn的相應值。

上面的過濾器有什麼問題嗎? 任何建議,將不勝感激。

回答

0

它看起來像你正在使用JNDI。

這裏是一個小樣本

//Create the initial directory context 
LdapContext ctx = new InitialLdapContext(env,null); 

//Create the search controls 
SearchControls ctls = new SearchControls(); 

//Specify the attributes to return 
String returnedAtts[]={"distinguishedName","CN","sAMAccountName"}; 
ctls.setReturningAttributes(returnedAtts); 

//Specify the search scope 
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 

//specify the LDAP search filter 
String searchFilter = "(&(objectClass=user)(sAMAccountName="+ theUserToCheck +"))"; 

//Specify the Base for the search 
String searchBase = "DC=erieinsurance,DC=com"; 

//initialize counter to total the results 
int totalResults = 0; 

//Search for objects using the filter 
NamingEnumeration answer = ctx.search(searchBase, searchFilter, ctls); 

//Loop through the search results 
while (answer.hasMoreElements()) { 
    SearchResult sr = (SearchResult)answer.next(); 
    totalResults++; 
    System.out.println(totalResults + ". " + sr.getName().toString()); 
} 
+0

感謝您的答覆......是的,我們正在使用JNDI。現在解決了搜索參數Id被映射到不同的參數,現在我已經映射到'CN'..因此它正在工作... –

+0

「theUserToCheck」對於LDAP過濾器注入(例如,如果有人指定了UserToCheck =「*)(otherAttr = otherValue」 –