我想使用Id从LDAP Active目录中获取一些用户信息。 这是我正在尝试连接并获取它的代码。
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 :(得分: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());
}