我正在尝试通过这段代码来获取特定组的成员列表:
List<String> usersList = new ArrayList<String>();
String[] attrIDs = { "distinguishedName","cn","name","uid",
"sn",
"givenname",
"memberOf",
"samaccountname",
"userPrincipalName" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(&(objectCategory=user)((memberOf=cn="+group_to_search+",ou=Groups,ou=XXX,dc=domain,dc=com)))";
NamingEnumeration answer = ldapDir.search( base, filter, ctls );
SearchResult rslt = (SearchResult)answer.next();
Attributes attrs = rslt.getAttributes();
String mo = String.valueOf(attrs.get("name"));
String[] groups = mo.split(",");
for(int i=0; i<groups.length; i++){
if(groups[i].contains("CN=")){
String user = groups[i].replace("CN=", "").replace("memberOf:", "").trim();
System.out.println(user);
usersList.add(group);
}
}
但是,尽管有20个用户,但似乎我只是获得group_to_search的第一个成员及其所有属性