|
既然找到了,转贴一个给大家看看: 下面四个JAVA方法,分别用于初始化,查询,添加,删除,修改,关闭连接。 记得每次都需要先大概连接,操作,然后关闭连接。和使用数据库差不多。 首先是需要用到的头文件: import java.util.Hashtable; import java.util.Enumeration; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchControls ; import javax.naming.NamingEnumeration; import javax.naming.directory.SearchResult; import javax.naming.directory.Attributes ; import javax.naming.directory.Attribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.ModificationItem; import java.lang.reflect.Method; import java.io.BufferedReader; import java.io.InputStreamReader; 然后是一个类域,用于保存上下文: DirContext ctx = null; 然后是初始化: public void init(){ String account="Admin";//操作LDAP的帐户。默认就是Admin。 String password="weblogic";//帐户Admin的密码。 String root="dc=ldapdomain"; //所操作的WLS域。也就是LDAP的根节点的DC Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");//必须这样写,无论用什么LDAP服务器。 env.put(Context.PROVIDER_URL, "ldap://localhost:7001/" + root);//LDAP服务器的地址:端口。对WLS端口就是7001 env.put(Context.SECURITY_AUTHENTICATION, "none");//授权界别,可以有三种授权级别,但是如果设为另外两种都无法登录,我也不知道为啥,但是只能设成这个值"none"。 env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root);//载入登陆帐户和登录密码 env.put(Context.SECURITY_CREDENTIALS, password); try{ ctx = new InitialDirContext(env);//初始化上下文 System.out.println("认证成功");//这里可以改成异常抛出。 }catch(javax.naming.AuthenticationException e){ System.out.println("认证失败"); }catch(Exception e){ System.out.println("认证出错:"+e); } } 查询操作: public void search(){//我只能按照某些属性查找节点,偶还不会怎么查找一个目录或按照更复杂的正则式查找特定节点/目录 try{ SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); System.out.print("what would you want to search:"); BufferedReader bd=new BufferedReader(new InputStreamReader(System.in)); String s=bd.readLine(); NamingEnumeration en = ctx.search("", "uid="+s, constraints); //要查询的UID。如果是*则可以查到所有UID的节点 if(en == null){ System.out.println("Have no NamingEnumeration."); } if(!en.hasMoreElements()){ System.out.println("Have no element."); } while (en != null && en.hasMoreElements()){//可以查出多个元素 Object obj = en.nextElement(); if(obj instanceof SearchResult){ SearchResult si = (SearchResult) obj; System.out.println(" name: " + si.getName()); Attributes attrs = si.getAttributes(); if (attrs == null){ System.out.println(" No attributes"); }else{ for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();){//获得该节点的所有属性 Attribute attr = (Attribute) ae.next();//下一属性 String attrId = attr.getID();//获得该属性的属性名 for (Enumeration vals = attr.getAll();vals.hasMoreElements();){//获得一个属性中的所有属性值 System.out.print(" "+attrId + ": "); Object o = vals.nextElement();//下一属性值 if(o instanceof byte[]) System.out.println(new String((byte[])o)); else System.out.println(o); } } } } else{ System.out.println(obj); } System.out.println(); } }catch(Exception e){ System.out.println("Exception in search():"+e); } } 添加操作: public void add(){ try{ String newUserName = "stella"; BasicAttributes attrs = new BasicAttributes(); BasicAttribute objclassSet = new BasicAttribute("objectclass"); objclassSet.add("person"); objclassSet.add("top"); objclassSet.add("organizationalPerson"); objclassSet.add("inetOrgPerson"); objclassSet.add("wlsUser"); attrs.put(objclassSet); attrs.put("sn", newUserName); attrs.put("uid", newUserName); attrs.put("cn", newUserName); ctx.createSubcontext("uid=" + newUserName+",ou=people,ou=myrealm", attrs); //添加一个节点,我还不会添加目录 }catch(Exception e){ System.out.println("Exception in add():"+e); } } 修改操作: public void edit(){ try{ String account = "stella";//修改以前旧的值 String sn = "stella sn";//修改以后新的值 ModificationItem modificationItem[] = new ModificationItem[1]; modificationItem[0] = new ModificationItem( DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("sn", sn));//所修改的属性 ctx.modifyAttributes("uid=" + account, modificationItem); //执行修改操作 }catch(Exception e){ System.out.println("Exception in edit():"+e); } } 删除节点操作: public void delete(){ try{ String uid = "stella"; String dc = "dc=it,dc=com"; ctx.destroySubcontext("uid=" + uid); //按照UID删除某个节点。我还不会删除一个目录。 }catch(Exception e){ System.out.println("Exception in edit():"+e); } } 关闭连接: public void close(){ if(ctx != null) { try { ctx.close(); } catch (NamingException e) { System.out.println("NamingException in close():"+e); } } } 我对LDAP的理解:它是用于对资源的管理和服务的访问协议,在Weblogic平台上的JNDI(包含EJB和DataSource)都是提供它来提供的。 正是JNDI的服务和RMI结合就形成J2EE平台上分布式的应用,因此说到底层,还是LDAP协议的支持。
|