package com.liang.test; import java.util.Hashtable; import java.util.Vector; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import javax.naming.ldap.InitialLdapContext; import javax.naming.ldap.LdapName; import org.opends.server.admin.client.ldap.JNDIDirContextAdaptor; public class OpenDSExamples { @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) { //DirContext dirContext= createLdapContext(); //连接OpenDS服务器 //JNDIDirContextAdaptor adaptor=JNDIDirContextAdaptor.adapt(dirContext); // do other stuff with the adaptor try{ String newUserName="world"; BasicAttributes attributes=new BasicAttributes(); BasicAttribute objclassSet=new BasicAttribute("objectclass"); objclassSet.add("person"); objclassSet.add("top"); objclassSet.add("organizationalPerson"); objclassSet.add("inetOrgPerson"); attributes.put(objclassSet); attributes.put("sn", newUserName); attributes.put("uid",newUserName); attributes.put("displayName","***********@163.com"); attributes.put("userPassword", "world"); LdapName dn=new LdapName("cn=world,dc=example,dc=com"); // 增加节点 //adaptor.createEntry(dn, attributes); //System.out.println("成功增加cn=world"); // 修改节点 //attributes.put("displayName", "管理员"); //adaptor.modifyEntry(dn, attributes); //System.out.println("成功修改cn=world"); //删除节点 // adaptor.deleteSubtree(dn); // System.out.println("成功删除cn=world"); // boolean isExist = adaptor.entryExists(dn); // if(isExist){ // System.out.println("查找:存在cn=world"); // } // else{ // System.out.println("查找:不存在cn=world"); // } // 查找节点 // SearchControls constraints=new SearchControls(); // // constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); // // boolean isExists=adaptor.entryExists(dn); // // if(isExists){ // System.out.println("查找:存在cn=world"); // }else{ // System.out.println("查找:不存在cn=world"); // } Vector ve=new 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()); NamingEnumeration<String> rsAttrsID=rs.getAttributes().getIDs(); while(rsAttrsID.hasMore()){ String attrName=rsAttrsID.nextElement(); System.err.print("属性名称:"+attrName+"++++++++++"); System.err.println("属性:"+rs.getAttributes().get(attrName)); } System.out.println("得到某一个属性-------"+rs.getAttributes().get("displayname")); if(db.trim().equals("")){ ve.add(dn); } else ve.add(db+","+dn); } System.err.println("共有几条记录----"+ve.size()); }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("rawtypes") private static 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", "rawtypes" }) 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", "rawtypes" }) private static DirContext createLdapContext() { Hashtable env=new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://127.0.0.1:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager"); env.put(Context.SECURITY_CREDENTIALS, "root"); DirContext ctx=null; try{ ctx=new InitialLdapContext(env, null); System.err.println("认证成功"); }catch(Exception e){ System.err.println("认证出错"); e.printStackTrace(); } return ctx; } }