作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须从 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/
我是一名优秀的程序员,十分优秀!