LDAP操作之java篇(1)

LDAP操作之java篇(1

       本文将分成4部分对LDAP操作进行讲解,每一部分都附带小例子(主要是参考MANNING - LDAP Programming, Management and Intergration一书,自己做了一些修改并通过验证)。阅读本文要有一定的LDAP知识,其实如果你自己配过LDAP服务器以后再来看这篇文章应该没什么难度(参见上一篇文章《在windows上配置openldap)

         连接LDAP服务器

String dn; //包括连接LDAP服务器的用户及要操作的根节点

DirContext dc; //相当于这个树的一个映像

Properties env = new Properties();

env.put(DirContext.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

env.put(DirContext.PROVIDER_URL, "ldap://" + host + ":" + port);

if (dn != null) {

       env.put(DirContext.SECURITY_PRINCIPAL, dn);

       env.put(DirContext.SECURITY_CREDENTIALS, password);

}

dc = new InitialDirContext(env);

 

dn举例: dn=” cn=Manager,dc=kkk,dc=com”,password就是这个dn对应的密码。HostLDAP服务器的主机地址,port是端口(openLDAP默认开放的是389)

 

         查询这棵树的所有节点

有点类似sql里边的select * from tableName;

                   /**

                    * @param base :根节点(在这里是”dc=kkk,dc=com”)

                    * @param scope :搜索范围

            * @param filter :指定子节点(格式为”(objectclass=*)”,*是指全部,你也可以指定某一特定类型的树节点)

            * @param attributes :属性集合(格式为{“*”}如果要指定搜索某一指定的属性列,就把*改成响应的属性列名称就行了)

                    * @return result result里边存的就是查询的结果集合

                    */

                   public Vector searchInformation(String base, String scope, String filter,

                             String[] attributes)

                   {

                      Vector results = new Vector();

                      SearchControls sc = new SearchControls();

                      if (scope.equals("base")) {

                             sc.setSearchScope(SearchControls.OBJECT_SCOPE);

                      } else if (scope.equals("one")) {

                             sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);

                      } else {

                             sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

                      }

                      // Reduce data provided by the LDAP server by listing

                      // only those attributes we want to return.

                      if (attributes.length > 0) {

                            sc.setReturningAttributes(attributes);

                     }

                      NamingEnumeration ne = null;

                      try {

                             ne = dc.search(base, filter, sc);

                             // Use the NamingEnumeration object to cycle through

                             // the result set.

                             while (ne.hasMore()) {

                                    Entry entry = new Entry();

                                    SearchResult sr = (SearchResult) ne.next();

                                   String name = sr.getName();

                                   if (base != null && !base.equals("")) {

                                           entry.setDN(name + "," + base);

                                    } else {

                                           entry.setDN(name);

                                    }

                                    Attributes at = sr.getAttributes();

                                    NamingEnumeration ane = at.getAll();

                                    while (ane.hasMore()) {

                                           Attribute attr = (Attribute) ane.next();

                                           String attrType = attr.getID();

                                           NamingEnumeration values = attr.getAll();

                                           Vector vals = new Vector();

                                           // Another NamingEnumeration object, this time

                                           // to iterate through attribute values.

                                           while (values.hasMore()) {

                                                  Object oneVal = values.nextElement();

                                                  if (oneVal instanceof String) {

                                                         vals.addElement((String) oneVal);

                                                  } else {

                                                         vals.addElement(new String((byte[]) oneVal));

                                                  }

                                           }

                                           entry.put(attrType, vals);

                                    }

                                    results.addElement(entry);

                             }

                             // The search() method can throw a number of exceptions.

                             // Here we just handle and print the exception.

                             // In real life we might want to pass the exception along

                             // to a piece of the software that might have a better

                             // context for correcting or presenting the problem.

                      } catch (Exception nex) {

                             System.err.println("Error: " + nex.getMessage());

                             nex.printStackTrace();

                             lc.close();

                      }

                      return results;

               }

               由于时间的关系,今天就先写到这,改天再把增加、修改、删除节点加上来:)

你可能感兴趣的:(LDAP)