Auslesen aus der ActiveDirectory

Mazee

Grünschnabel
Hallo zusammen,

ich habe folgende Frage. Ich möchte Daten aus der AD abfragen, das klappt auch alles. Mit diesem Code kriege ich folgendes:

CN=Walter,OU=MAR,OU=Users-KE,OU=Users-BC,OU=ALL Users: null:null:{givenname=givenName: Manuel, sn=sn: Walter}

Wunderbar, das find ich auch toll. Doch leider interessiert mich das alles nicht. Er soll mir nur die Attribute anzeigen.

Das heißt: Name: Walter Vorname: Manuel

Weiß jemand wie das geht?

Vielen Dank

Java:
package Tool;
import java.util.ArrayList;
  import java.util.Hashtable;
  import java.util.Iterator;
  import java.util.List;
  import javax.naming.*;
  import javax.naming.directory.*;
  import javax.naming.Context;
  import javax.naming.NamingEnumeration;
  import javax.naming.directory.SearchControls;
  import javax.naming.directory.SearchResult;
  import javax.naming.ldap.InitialLdapContext;
  import javax.naming.ldap.LdapContext;
  import java.io.*;
  
  public class ActiveDirectorySearchExample {
  
  	final static String ADMIN_NAME = "Administrator@bc.cmc.ad";
  
  	final static String ADMIN_PASSWORD = "XXX";
  
  	final static String SEARCH_BASE = "DC=bc,DC=cmc,DC=ad";
  
  	static LdapContext ctx;
  
  	/**
  	 * @param args
  	 */
  	public static void main(String[] args) throws Exception 
        {
            BufferedReader in = new BufferedReader(
                                   new InputStreamReader(System.in));
  		init();
  		System.out.println("Bitte name eingeben: ");
                String name =  in.readLine();
                
                List list = findUsersByAccountName(name);
  		for (Iterator iter = list.iterator(); iter.hasNext();) {
  			String element = (String) iter.next();
  			System.out.println(element);
  		}
  		ctx.close();
  	}
  
  	static void init() throws Exception {
  		Hashtable env = new Hashtable();
  		env.put(Context.INITIAL_CONTEXT_FACTORY,
 				"com.sun.jndi.ldap.LdapCtxFactory");
  		env.put(Context.SECURITY_AUTHENTICATION, "simple");
  		env.put(Context.SECURITY_PRINCIPAL, ADMIN_NAME);
  		env.put(Context.SECURITY_CREDENTIALS, ADMIN_PASSWORD);
  		//Der entsprechende Domänen-Controller
  		env.put(Context.PROVIDER_URL, "ldap://192.168.45.103:389");
  		ctx = new InitialLdapContext(env, null);
  	}
  
  	static List findUsersByAccountName(String accountName) throws Exception{
  		List list = new ArrayList();
  
  		String snPrefix = accountName + "*";
  		String givenNamePrefix = accountName + "*";
  
  		//LDAP Abfrage...
  		String searchFilter = "(|(sn=" + snPrefix
 				+ ")(givenName=" + givenNamePrefix + "))";
  
  		  
  		SearchControls searchControls = new SearchControls();
                String[] resultAttributes = {"sn", "givenName"};
  		searchControls.setReturningAttributes(resultAttributes);
  		searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  		               
  		NamingEnumeration results = ctx.search(SEARCH_BASE, searchFilter,
  				searchControls);
  		
  		while(results.hasMoreElements()){
 			
                        SearchResult searchResult = (SearchResult)results.nextElement();
                      
                        //Attribute att = (Attribute)searchResult.getAttributes();
  			//System.out.println("attribute: " + att.getID());
                        //list.add(att.toString());
                        list.add(searchResult.toString());
  		}
  		return list;
  	}
  }
 
Zurück