JAVA LADP AD 도 메 인 사용자 목록 정보 가 져 오기

8291 단어
import java.util.Enumeration;
import java.util.Hashtable;

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.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class ADOperTest {
 public ADOperTest() {
 }
 public void GetADInfo() {
  String userName = "xxxx"; //     
  String passwd = "xxx";
  String host = "192.168.100.14"; // AD   
  String port = "389"; //   
  String domain = "@xxx.com"; //       
  String url = new String("ldap://" + host + ":" + port);
  String user = userName.indexOf(domain) > 0 ? userName : userName
    + domain;
  Hashtable HashEnv = new Hashtable();
  // String adminName ="CN=oyxiaoyuanxy,CN=Users,DC=Hebmc,DC=com";//AD    
  String adminName = "xueqiang"; //         :domain\User  
  // [email protected]
  adminName = "xueqiang"; //         :domain\User   [email protected]
  String adminPassword = "123456"; //   
  HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP      
  HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User
  HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); // AD Password
  HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP   
  HashEnv.put(Context.PROVIDER_URL, url);
  try {
   LdapContext ctx = new InitialLdapContext(HashEnv, null);
   //    
   String searchBase = "DC=wanda-dev,DC=cn";
   // LDAP      
   String searchFilter = "objectClass=User";
   //      
   SearchControls searchCtls = new SearchControls(); // Create the
   // search
   // controls
   //        
   searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify
   // the
   // search
   // scope
   //       
   // searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE); //
   // Specify the search scope       
   String returnedAtts[] = { "memberOf", "distinguishedName",
     "Pwd-Last-Set", "User-Password", "cn" };//       
   // String returnedAtts[] = { "url", "whenChanged", "employeeID",
   // "name", "userPrincipalName", "physicalDeliveryOfficeName",
   // "departmentNumber", "telephoneNumber", "homePhone",
   // "mobile", "department", "sAMAccountName", "whenChanged",
   // "mail" }; //       
   searchCtls.setReturningAttributes(returnedAtts); //        
   //         、            LDAP    
   NamingEnumeration answer = ctx.search(searchBase, searchFilter,
     searchCtls);// Search for objects using the filter
   //          0
   int totalResults = 0;// Specify the attributes to return
   int rows = 0;
   while (answer.hasMoreElements()) {//      
    SearchResult sr = (SearchResult) answer.next();//          DN
    System.out.println(++rows
      + "************************************************");
    System.out.println(sr.getName());
    Attributes Attrs = sr.getAttributes();//           
    if (Attrs != null) {
     try {
      for (NamingEnumeration ne = Attrs.getAll(); ne
        .hasMore();) {
       Attribute Attr = (Attribute) ne.next();//        
       System.out.println(" AttributeID=   :"
         + Attr.getID().toString());
       //      
       for (NamingEnumeration e = Attr.getAll(); e
         .hasMore(); totalResults++) {
        System.out.println("    AttributeValues=   :"
          + e.next().toString());
       }
       System.out.println("    ---------------");
       //      
       // Enumeration values = Attr.getAll();
       // if (values != null) { //   
       // while (values.hasMoreElements()) {
       // System.out.println("    AttributeValues=   :"+
       // values.nextElement());
       // }
       // }
       // System.out.println("    ---------------");
      }
     } catch (NamingException e) {
      System.err.println("Throw Exception : " + e);
     }
    }
   }
   System.out
     .println("************************************************");
   System.out.println("Number: " + totalResults);
   ctx.close();
  } catch (NamingException e) {
   e.printStackTrace();
   System.err.println("Throw Exception : " + e);
  }
 }
/* public static void main(String args[]) {
  //    
  ADOperTest ad = new ADOperTest();
  ad.GetADInfo();
 }*/
 
 public static void main(String args[]) {
	    Hashtable HashEnv = new Hashtable();

	    String LDAP_URL = "ldap://xxx.com:389"; //LDAP    
	    String adminName = "[email protected]"; //        :domain\User   [email protected]
	    String adminPassword = "xxxx"; //  

	    HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); //LDAP      
	    HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); //AD User
	    HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); //AD Password
	    HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); //LDAP   
	    HashEnv.put(Context.PROVIDER_URL, LDAP_URL);

	    try {
	      LdapContext ctx = new InitialLdapContext(HashEnv, null);
	      SearchControls searchCtls = new SearchControls(); //Create the search controls
	      searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); //Specify the search scope

	      String searchFilter = "objectClass=User"; //specify the LDAP search filter
	    
	      String searchBase = "DC=xxx,DC=com"; //Specify the Base for the search//     
	      int totalResults = 0;

	           String returnedAtts[] = {
	          "url", "whenChanged", "employeeID", "name", "userPrincipalName",
	          "physicalDeliveryOfficeName", "departmentNumber", "telephoneNumber",
	          "homePhone", "mobile", "department", "sAMAccountName", "whenChanged",
	          "mail"}; //      

	      searchCtls.setReturningAttributes(returnedAtts); //       

	      //Search for objects using the filter
	      NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls);
	      
	      if(answer==null||answer.equals(null)){
	    	  System.out.println("answer is null");
	      }else{
	    	  System.out.println("answer not null");
	      }
	      
	      System.out.println(answer.hasMoreElements());
	      
	      while (answer.hasMoreElements()) {
	        SearchResult sr = (SearchResult) answer.next();
	        System.out.println("************************************************");
	        System.out.println(sr.getName());

	        Attributes Attrs = sr.getAttributes();
	        if (Attrs != null) {
	          try {
	            for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore(); ) {
	              Attribute Attr = (Attribute) ne.next();

	              System.out.println(" AttributeID=" + Attr.getID().toString());

	              //     
	              for (NamingEnumeration e = Attr.getAll(); e.hasMore();totalResults++) {
	                System.out.println("    AttributeValues=" + e.next().toString());
	              }
	              System.out.println("    ---------------");

	              //     
	              Enumeration values = Attr.getAll();
	              if (values != null) { //   
	                while (values.hasMoreElements()) {
	                  System.out.println("    AttributeValues=" + values.nextElement());
	                }
	              }
	              System.out.println("    ---------------");
	            }
	          }
	          catch (NamingException e) {
	            System.err.println("Throw Exception : " + e);
	          }
	        }
	      }
	      System.out.println("Number: " + totalResults);
	      ctx.close();
	    }

	    catch (NamingException e) {
	      e.printStackTrace();
	      System.err.println("Throw Exception : " + e);
	    }
	}
}

좋은 웹페이지 즐겨찾기