0

我想使用 Id 从 LDAP Active Directory 中获取一些用户信息。这是我试图连接并获取它的代码。

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 的对应值。

上面的过滤器有什么问题吗?任何建议将不胜感激。

4

1 回答 1

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());
}
于 2011-11-09T05:09:00.167 回答