gpt4 book ai didi

java - 如何从 Ldap 中的给定组中提取所有成员的电子邮件地址

转载 作者:行者123 更新时间:2023-12-01 19:21:05 25 4
gpt4 key购买 nike

我必须从 LDAP 中提取群组所有成员的电子邮件 ID。我的群组名称是 reportMember (String groupName=reportMember;),我想获取该群组的所有用户及其电子邮件地址。所以我写了一个代码来获取它。我提供的查询为:“String searchFilter="cn="+groupName;” 该查询为我提供了该组所有成员的姓名:

member1 :CN=Alex,OU=InfoWorker,OU=People,DC=abc,DC=xyz,DC=com

成员2:............等等。

但是通过这个查询,我将无法获取我的代码如下的那些成员的电子邮件地址。

提前非常感谢。

代码:

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");

关于java - 如何从 Ldap 中的给定组中提取所有成员的电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59353447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com