Hallo zusammen,
ich habe mir den code von einem Programm aus dem Internet geschnappt und bearbeitet, mit der Hoffnung das ich mich am Ldap-Server anmelden kann. Doch leider bringt er mir immer eine Fehlermeldung. Woran kann es liegen?
package Tool;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
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;
public class ActiveDirectorySearchExample {
final static String ADMIN_NAME = "CN=WalterM. OU=Admins, OU=All Users";
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 {
init();
List list = findUsersByAccountName("");
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://dc-bc-ke01:389");
ctx = new InitialLdapContext(env, null);
}
static List findUsersByAccountName(String accountName) throws Exception{
List list = new ArrayList();
String snPrefix = accountName.substring(0, 2) + "*";
String givenNamePrefix = accountName.substring(2) + "*";
//Unsere LDAP Abfrage...
String searchFilter = "(&(objectClass=user)(sn=" + snPrefix
+ ")(givenName=" + givenNamePrefix + "))";
//System.out.println(searchFilter);
SearchControls searchControls = new SearchControls();
String[] resultAttributes = { "sn", "givenName", "sAMAccountName" };
searchControls.setReturningAttributes(resultAttributes);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search(SEARCH_BASE, searchFilter,
searchControls);
while(results.hasMoreElements()){
SearchResult searchResult = (SearchResult)results.nextElement();
list.add(searchResult.toString());
}
return list;
}
}
ich habe mir den code von einem Programm aus dem Internet geschnappt und bearbeitet, mit der Hoffnung das ich mich am Ldap-Server anmelden kann. Doch leider bringt er mir immer eine Fehlermeldung. Woran kann es liegen?
package Tool;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
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;
public class ActiveDirectorySearchExample {
final static String ADMIN_NAME = "CN=WalterM. OU=Admins, OU=All Users";
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 {
init();
List list = findUsersByAccountName("");
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://dc-bc-ke01:389");
ctx = new InitialLdapContext(env, null);
}
static List findUsersByAccountName(String accountName) throws Exception{
List list = new ArrayList();
String snPrefix = accountName.substring(0, 2) + "*";
String givenNamePrefix = accountName.substring(2) + "*";
//Unsere LDAP Abfrage...
String searchFilter = "(&(objectClass=user)(sn=" + snPrefix
+ ")(givenName=" + givenNamePrefix + "))";
//System.out.println(searchFilter);
SearchControls searchControls = new SearchControls();
String[] resultAttributes = { "sn", "givenName", "sAMAccountName" };
searchControls.setReturningAttributes(resultAttributes);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search(SEARCH_BASE, searchFilter,
searchControls);
while(results.hasMoreElements()){
SearchResult searchResult = (SearchResult)results.nextElement();
list.add(searchResult.toString());
}
return list;
}
}