java – 列出Active Directory组中的所有用户

列出Active Directory中的所有用户时,我遇到了一些困难.我到达了小组,但不幸的是,我无法检索所有用户.我正在寻找用户全名,用户名,董事会.我的代码是:

package client;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.*;   
import java.util.Enumeration;

import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.directory.*; 

public class AD1 {
    public AD1() {
        super();
    }

    public static String usersContainer = "cn=XX,ou=XX,ou=Groups,dc=XX,dc=XX,dc=XXX";

    public static void main(String[] args) {

        try {
            LdapContext ctx = null;
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.SECURITY_AUTHENTICATION, "Simple");
            //it can be <domain\\userid> something that you use for windows login
            //it can also be
            env.put(Context.SECURITY_PRINCIPAL, "username");
            // env.put(Context.SECURITY_CREDENTIALS, "password");
            try {
                env.put(Context.SECURITY_CREDENTIALS, "password".getBytes("UTF8"));
            }
            catch (java.io.UnsupportedEncodingException e) { /* ignore */ }

            //in following property we specify ldap protocol and connection url.
            //generally the port is 389
            env.put(Context.PROVIDER_URL, "ldap://IP:PORT");

            ctx = new InitialLdapContext(env, null);

            System.out.println("Connection Successful.");

            DirContext ctx1 = new InitialDirContext(env);   
            SearchControls ctls = new SearchControls();        
            String[] attrIDs = { "distinguishedName","cn","name","uid",
                                 "sn",
                                 "givenname",
                                 "memberOf",
                                 "samaccountname",
                                 "userPrincipalName" };

            ctls.setReturningAttributes(attrIDs);       
            ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
            //ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
            NamingEnumeration answer = ctx1.search( usersContainer, "(objectClass=user)", ctls );     

            System.out.print("Out while loop");
            while(answer.hasMore()) {
                System.out.print("while loop11");
                SearchResult rslt = (SearchResult)answer.next();          
                Attributes attrs = rslt.getAttributes();           
                System.out.println(attrs.get("uid"));         
                ctx1.close(); 
            }

        } catch(NamingException nex) {
            System.out.println("Active Directory Connection: FAILED");
            nex.printStackTrace();
        }
    }
}

谁能帮忙.

最佳答案 因为它是活动目录而不是你真正讨论的ldap,你能帮我们更多关于ldap / active目录配置的细节吗?

你提供的信息很难帮助你

编辑1:
你的问题在这里没有?

while(answer.hasMore()) {
     System.out.print("while loop11");
     SearchResult rslt = (SearchResult)answer.next();          
     Attributes attrs = rslt.getAttributes();           
     //System.out.println(attrs.get("uid"));
     System.out.println(attrs.get("cn"));
     ctx1.close(); 
     }
点赞