· df · 1 min read

ldap and Java

InitialDirContext ldapjava and ldapTechnologySearchControls ldapldap code

For those who are starters with LDAP, a good article can be found in Wikipedia.  The LDAP URL is fairly extensible and a sample format is

ldap://host:port/DN?Atributes_to_Fetch?range?SearchFilter?extensions

  • hostname is the Fully Qualified (FQDN) or IP address of the LDAP server to search.

  • port is the network port (default port 389) of the LDAP server.

  • DN is the distinguished name to use as the search base.

  • Atributes_to_Fetch is comma-separated.

  • range specifies the search scope and can be “base” (the default), “one” or “sub”.

  • SearchFilter is a search critiera. Can accept wild characters too

  • extensions are optional.



A sample ldap program in Java
import javax.naming.directory.;
import javax.naming.
;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Properties;
public class search {
public static void main(String[] args) {
String base = “cn=generic,ou=MyClient,ou=Applications,ou=Pacific,o=mycompany.net”;
String filter = “(uniquemember=uid=myuserid,ou=Users,ou=Pacific,o=mycompany.net)”;
Properties env = new Properties();
env.put(DirContext.INITIAL_CONTEXT_FACTORY,   “com.sun.jndi.ldap.LdapCtxFactory”);
env.put(DirContext.PROVIDER_URL,“ldap://10.123.34.56:44389”);
env.put( Context.SECURITY_PRINCIPAL, “uid=myuserid,ou=Users,ou=Pacific,o=mycompany.net” );
env.put( Context.SECURITY_CREDENTIALS, “passkey” );

try {
DirContext dc = new InitialDirContext(env);
SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
NamingEnumeration ne = null;
ne = dc.search(base, filter, sc);
while (ne.hasMore()) {
SearchResult sr = (SearchResult) ne.next();
System.out.println(sr.toString()+“\n”);
}
dc.close();
} catch (NamingException nex) {
System.err.println(“Error: “ + nex.getMessage());
}
}
}

Print’s Details of the directory structure

 

[ad#ad-2]