import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Set up the environment for creating the initial context
Hashtable<String, String> env = new Hashtable<String, String>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=admin, dc=univ-lehavre, dc=fr");
env.put(Context.SECURITY_CREDENTIALS, "secret");
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
Attributes matchAttrs = new BasicAttributes(true);
//matchAttrs.put(new BasicAttribute("cn", "alenorcy"));
matchAttrs.put(new BasicAttribute("uid"));
// Search for objects that have those matching attributes
NamingEnumeration<?> answer =
ctx.search("ou=users,dc=univ-lehavre, dc=fr", matchAttrs);
while (answer.hasMore()) {
SearchResult sr = (SearchResult)answer.next();
System.out.println(">>>" + sr.getName());
System.out.println(sr.getAttributes());
}
// Close the context when we're done
ctx.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}