添加操作: // Standard anonymous search import java.util.Hashtable; import java.util.Enumeration; import javax.naming.*; import javax.naming.directory.*; public class JNDIAdd { // Initial context implementation public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory "; public static String MY_HOST = "ldap://localhost:389 "; public static String MGR_DN = "uid=kvaughan, ou=People, dc=siroe, dc=com "; public static String MGR_PW = "bribery "; public static String MY_SEARCHBASE = "dc=siroe,dc=com "; //public static String MY_BASE = "ou=people,o=airius.com "; //public static String MY_DN = "uid=mewilcox "; public static void main(String args[]) { try { // Hashtable for environmental information Hashtable env = new Hashtable(); // Specify which class to use for our JNDI provider env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX); // Specify host and port to use for directory service env.put(Context.PROVIDER_URL, MY_HOST); env.put(Context.SECURITY_AUTHENTICATION, "simple "); env.put(Context.SECURITY_PRINCIPAL,MGR_DN); env.put(Context.SECURITY_CREDENTIALS,MGR_PW); // Get a reference to a directory context DirContext ctx = new InitialDirContext(env); Person p = new Person( "mewilcox ", "Mark ", "Wilcox ", "ou=Accounting ", "[email protected] "); ctx.bind( "uid=mewilcox,ou=People,dc=siroe,dc=com ", p); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } 删除操作: import java.util.Hashtable; import java.util.Enumeration; import javax.naming.*; import javax.naming.directory.*; public class JNDIDel { // initial context implementation public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory "; public static String MY_HOST = "ldap://localhost:389 "; public static String MGR_DN = "uid=kvaughan, ou=People, dc=siroe, dc=com "; public static String MGR_PW = "bribery "; public static String MY_SEARCHBASE = "dc=siroe,dc=com "; public static String MY_ENTRY = "uid=mewilcox, ou=People, dc=siroe, dc=com "; public static void main(String args[]) { try { //Hashtable for environmental information Hashtable env = new Hashtable(); //Specify which class to use for our JNDI provider env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX); env.put(Context.PROVIDER_URL,MY_HOST); env.put(Context.SECURITY_AUTHENTICATION, "simple "); env.put(Context.SECURITY_PRINCIPAL,MGR_DN); env.put(Context.SECURITY_CREDENTIALS,MGR_PW); //Get a reference to a directory context DirContext ctx = new InitialDirContext(env); ctx.destroySubcontext(MY_ENTRY); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }
搜索操作: // Standard anonymous search import java.util.Hashtable; import java.util.Enumeration; import javax.naming.*; import javax.naming.directory.*; public class JNDISearch { // Initial context implementation public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory "; public static String MY_HOST = "ldap://localhost:389 "; public static String MY_SEARCHBASE = "dc=siroe,dc=com "; public static String MY_FILTER = "(sn=Carter) "; public static void main(String args[]) { try { // Hashtable for environmental information Hashtable env = new Hashtable(); // Specify which class to use for our JNDI provider env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX); // Specify host and port to use for directory service env.put(Context.PROVIDER_URL, MY_HOST); // Get a reference to a directory context DirContext ctx = new InitialDirContext(env); // Specify the scope of the search SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); // Perform the actual search // We give it a searchbase, a filter and a the constraints // containing the scope of the search NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints); // Now step through the search results while (results != null && results.hasMore()) { SearchResult sr = (SearchResult) results.next(); String dn = sr.getName(); System.out.println( "Distinguished Name is " + dn); Attributes attrs = sr.getAttributes(); for (NamingEnumeration ne = attrs.getAll(); ne.hasMoreElements(); ) { Attribute attr = (Attribute) ne.next(); String attrID = attr.getID(); System.out.println(attrID + ": "); for (Enumeration vals = attr.getAll(); vals.hasMoreElements(); ) { System.out.println( "\t " + vals.nextElement()); } } System.out.println( "\n "); } } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }