我正在使用一个Java程序从活动目录中检索用户信息。该程序正在运行,但仅传递一组数据(搜索过滤器定义为字符串)
String searchFilter ="CN=CostCentre_1041";)
如何在程序中传递多个组。

我尝试使用数组,但无法正确定义它。

package Test.ad;

import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class GetUsersFrormLDAPGroup {
    static String ldapSearchBase = "DC=test,DC=edu,DC=com";
    private static DirContext ctx = null;

    private static DirContext getActiveDirectoryContext() throws Exception {
        final Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        properties.put(Context.PROVIDER_URL, "ldap://testad:3268");
        properties.put(Context.SECURITY_AUTHENTICATION, "simple");
        properties.put(Context.SECURITY_PRINCIPAL, "admin");
        properties.put(Context.SECURITY_CREDENTIALS, "test");
        return new InitialDirContext(properties);
    }

    public void getGroupUsers(String searchBase, String searchFilter, String returnedAttrs[], int maxResults) {
        Hashtable userEntries = null;
        String member = "";
        try {
            SearchControls searchCtls = new SearchControls();
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            searchCtls.setReturningAttributes(returnedAttrs);
            ctx = getActiveDirectoryContext();
            try {
                System.out.println("Search Base: " + searchBase);
                System.out.println("Search Filter: " + searchFilter);
                NamingEnumeration users = ctx.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 = "";
                userEntries = new Hashtable();
                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 " + returnedAttrs[0] + " for this object.");
                    } else {

                        try {
                            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);
                                    }
                                    //{
                                    //System.out.println("empty");
                                    // }
                                }
                            }
                        } catch (NamingException e) {
                            System.out.println("Problem listing membership:" + e);
                        }
                    }
                    k++;
                }
            } catch (NamingException e) {
                System.out.println("Problem searching directory: " + e);
            }
            ctx.close();
            ctx = null;
        } catch (Exception namEx) {
            System.out.println("Exception while fetching the users from LDAP::" + namEx);
        }

    }

    public static void main(String args[]) throws Exception {
        GetUsersFrormLDAPGroup gug = new GetUsersFrormLDAPGroup();
        String returnedAttrs[] = {"cn", "member", "name"};
        String searchFilter = "CN=CostCentre_1041";
        gug.getGroupUsers(ldapSearchBase, searchFilter, returnedAttrs, Integer.parseInt("2000"));
    }
}

最佳答案

您可以:


第二次以不同的getGroupUsers()调用searchFilter以获取不同组的结果:




String searchFilter = "CN=CostCentre_1041";
gug.getGroupUsers(ldapSearchBase, searchFilter, returnedAttrs, Integer.parseInt("2000"));

searchFilter = "CN=SNOW - EA DEV INTG";
gug.getGroupUsers(ldapSearchBase, searchFilter, returnedAttrs, Integer.parseInt("2000"));



更改您的searchFilter以返回两个组。




String searchFilter = "(|(CN=CostCentre_1041)(CN=SNOW - EA DEV INTG))";
gug.getGroupUsers(ldapSearchBase, searchFilter, returnedAttrs, Integer.parseInt("2000"));

09-26 03:33