---------------------------------------------------------------- 备忘-----------------------------------------------------------------------
public class OpenDSExamples { public static void main(String[] args) throws NamingException { DirContext dirContext = createLdapContext();// 连接OpenDS服务器 JNDIDirContextAdaptor adaptor = JNDIDirContextAdaptor.adapt(dirContext); // do other stuff with the adaptor try { String newUserName = "admin"; BasicAttributes attrsbu = new BasicAttributes(); BasicAttribute objclassSet = new BasicAttribute("objectclass"); objclassSet.add("person"); objclassSet.add("top"); objclassSet.add("organizationalPerson"); objclassSet.add("inetOrgPerson"); attrsbu.put(objclassSet); attrsbu.put("sn", newUserName); attrsbu.put("uid", newUserName); attrsbu.put("displayName", "********@163.com"); attrsbu.put("userPassword", "Administrator"); LdapName dn = new LdapName("cn=admin,dc=example,dc=com"); // 增加节点 //adaptor.createEntry(dn, attrsbu); System.out.println("成功增加cn=admin"); // 修改节点 //attrsbu.put("displayName", "管理员"); //adaptor.modifyEntry(dn, attrsbu); System.out.println("成功修改cn=admin"); // 查找节点 SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); boolean isExists = adaptor.entryExists(dn); if (isExists) { System.out.println("查找:存在cn=admin"); } else { System.out.println("查找:不存在cn=admin"); } java.util.Vector ve = new java.util.Vector(); NamingEnumeration em = getFilter("dc=example,dc=com", "cn=*", 10); while (em != null && em.hasMoreElements()) { SearchResult rs = (SearchResult) em.nextElement(); String db = rs.getName(); System.out.println("名称=====" + rs.getName()); System.out.println("全部属性====" + rs.getAttributes()); System.out.println("得到某一个属性====" + rs.getAttributes().get("displayname")); if (db.trim().equals("")) ve.add(dn); else ve.add(db + "," + dn); } System.out.println("共有几条记录====" + ve.size()); /* * //删除节点 * * adaptor.deleteSubtree(dn); * * System.out.println("成功删除cn=admin"); * * isExists = adaptor.entryExists(dn); * * if(isExists){ * * System.out.println("查找:存在cn=admin"); * } * * else{ * * System.out.println("查找:不存在cn=admin"); * } */ } catch (Exception e) { System.out.println("adaptor操作cn=admin出错"); e.printStackTrace(); } } /** * 返回用户查询接口 * @param DN 查找范围 ou=test,dc=abcd,dc=com * @param filter 过滤条件 cn=* * @param myserach 搜索范围 * @return LDAP标准接口 * @throws Exception */ @SuppressWarnings("unchecked") public static javax.naming.NamingEnumeration getFilter(String DN, String filter, int myserach) throws Exception { DirContext ctx = createLdapContext(); try { NamingEnumeration em; SearchControls con = new SearchControls(); con.setSearchScope(myserach); em = ctx.search(DN, filter, con); return em; } finally { // } } /** * 设置条件查找 * @param dn 查找的根结点 * @param filter 查找条件 * @param level 查找范围 * @return 返回一个由DN组成的JAVA STRING 列表 * @throws Exception */ @SuppressWarnings("unchecked") public java.util.Vector getfilterNodes(String dn, String filter, int level) throws Exception { java.util.Vector ve = new java.util.Vector(); NamingEnumeration em = getFilter(dn, filter, level); while (em != null && em.hasMoreElements()) { SearchResult rs = (SearchResult) em.nextElement(); String db = rs.getName(); if (db.trim().equals("")) ve.add(dn); else ve.add(db + "," + dn); } return ve; } @SuppressWarnings("unchecked") private static DirContext createLdapContext() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://192.168.1.102:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=root"); env.put(Context.SECURITY_CREDENTIALS, "root"); DirContext ctx = null; try { ctx = new InitialLdapContext(env, null); System.out.println("认证成功"); } catch (Exception e) { System.out.println("认证出错:"); e.printStackTrace(); } return ctx; } }
-------------------------------------------------------程序来源网络 但是都经过本人测试---------------------------------------------------------------------