我必须从LDAP提取组中所有成员的电子邮件ID。
我的群组名称是reportMember(字符串groupName = reportMember;),我想通过该群组的电子邮件地址获取该群组的所有用户。所以我写了一个代码来得到它。
我提供的查询为:“ String searchFilter =” cn =“ + groupName;”该查询为我提供了该组所有成员的名称:
member1:CN = Alex,OU = InfoWorker,OU =人,DC = abc,DC = xyz,DC = com
member2:......等等。
但是通过此查询,我将无法获得我的代码在下面的那些成员的电子邮件地址。
提前谢谢了。
码:
public List<LDAPUser> searchGroupDetails(String currentUserName, String groupName) {
List<LDAPUser> LDAPUsers=new ArrayList<LDAPUser>();
ArrayList<String> listOfMembersInGroup= new ArrayList<String>();
int maxResults=Integer.parseInt("2000");
DirContext dirSearchContext = utilusr.getLDAPDirContext().get(currentUserName);
String searchbase = "DC=abc,DC=xyz,DC=com";
String searchFilter="cn="+groupName;
String member="";
try{
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchCtls.setReturningAttributes(returnAttributes);
try{
NamingEnumeration<?> users = dirSearchContext.search(searchbase, searchFilter, searchCtls);
if(users.hasMoreElements() == false){
System.out.println("Not find any object with this filter " + searchFilter + " and searchBase " + searchbase);
}
int k = 0;
String attValue = "";
while (users.hasMoreElements()){
if(k >= maxResults)
break;
SearchResult sr = (SearchResult)users.next();
Attributes attrs = sr.getAttributes();
if (attrs.size() == 0){
System.out.println("Could not find attribute " + returnAttributes[0] + " for this object.");
}else{
try{
//-- Code to extract members of a given group Start.
for (NamingEnumeration<?> ae = attrs.getAll();ae.hasMore();){
Attribute attr = (Attribute)ae.next();
String id = attr.getID();
for (NamingEnumeration<?> e = attr.getAll();e.hasMore();){
attValue = (String)e.next();
if(id.equalsIgnoreCase("member")){
member = attValue;
System.out.println("member :"+member);
String memberName=member.substring(member.indexOf("=")+1, member.indexOf(","));
listOfMembersInGroup.add(memberName);
}
//-- Code to extract members of a given group Ends.
else
{
System.out.println("empty");
}
}
}
}catch(NamingException e){
e.printStackTrace();
}
}
k++;
}
}catch (NamingException e){
e.printStackTrace();
}
dirSearchContext=null;
}catch (Exception e){
e.printStackTrace();
}
}
最佳答案
假设Microsoft Active Directory(或memberOF是否可用,因为您没有透露)
与此类似的东西应该可以工作(当然要与您的环境模块兼容)
String[] attrIDs = {"mail"};
String groupName = "reportMember";
String groupDN = "CN=Groups,DC=abc,DC=xyz,DC=com"; // Where groups are in LDAP
Attributes matchAttrs = new BasicAttributes(true); // ignore case
matchAttrs.put(new BasicAttribute("memberOf", "CN=" + groupName + ","+ groupDN)); // finds group by CN Value
String searchFilter = "(&(objectCategory=Person)(objectClass=User)(memberOf=" + groupDN + "))";
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ctx.search(ldapSearchBase, searchFilter, searchControls, attrIDs);
while (results.hasMore()) {
SearchResult result = results.next();
Attributes attributes = result.getAttributes();
Attribute email = attributes.get("mail");
System.out.println(email);
//get/iterate the values of the attribute
}
System.out.println("DONE");