LDAP应用技术简述


编写LDAP访问程序;

a)        JAVA

                        i.              JNDI(JAVA 命名及目录接口)

JNDI是JAVA为命名及目录服务访问制定的基础接口标准,用于访问包括DNS,NIS,LDAP,文件系统等任何以树目录形式存在目标对象,并且基本上可保持访问方式的一致(意味着代码共用)。对于不同的目录类型,JNDI通过使用不同的目录驱动,以保证访问方式的一致。

以下连接可获得较详细的讲解和例程:http://java.sun.com/products/jndi/tutorial/

经常使用的LDAP驱动有JDK自带的LDAP provider,还有IBM的PROVIDER,以及NOVEL的JNDI产品。需要提醒的是,JNDI中最重要的概念是上下文context,即定位从那一个入口开始操作,相当于openldap命令行中的-D开关的作用。其他的操作都是该上下文对象的调用。

LDAP通过JNDI添加条目是基于DirContext类的操作。由于JAVA只能以对象方式向LDAP添加条目,因此,必须首先具备实现DirContext接口的类,或使用DirContext(扩充了context接口)代替context,然后才能通过ctx.bind()的方法把该类添加到目录中。

JAVA-LDAP-ADD的操作可以有三种方式:

Context.bind()或ctx.createSubcontext("name");

或DirContext.bind(“dn”,attrs,object)的方式。对于没有预先编写实现DirContext接口的类对象的添加,这是唯一的办法。

                       ii.              JLDAP

JLDAP是由novel开发的,原是针对Novel的NDS目录设计的JAVA访问工具。NOVEL的NDS和网景(NETSCAPE)的目录是工具界最早的目录产品。JLDAP并非JNDI的服务供应者,而是同一抽象层次下的访问工具集。与JNDI-LDAP相比,JLDAP更接近于类关系数据库的访问方式。

NDS是遵守LDAP协议的并进行了扩展的类MAD产品。而NOVEL也已把JLDAP捐献给了OPENLDAP开源项目,可以世界范围内自由使用。与JNDI相比,JLDAP无须继承DirContext才能实现添加,也无需预先生成添加的类,可以象普通数据访问那样,生成连接,然后使用::add方法添加。这样,添加的灵活性要强于JNDI。

但由于JLDAP目前是访问NDS,因此,它不具备JNDI完全面向对象存储的能力,对于高级的LDAP应用,JLDAP不是合适的选择。

例:


java 代码


import com.novell.ldap.*;    public class AddEntry    {        public static void main( String[] args )        {            if (args.length != 4) {                System.err.println("Usage:   java AddEntry                                                     + "   " );                System.err.println("Example: java AddEntry Acme.com"                            + " \"cn=admin,o=Acme\" secret \"ou=Sales,o=Acme\"");                System.exit(1);            }                               int ldapPort = LDAPConnection.DEFAULT_PORT;            int ldapVersion  = LDAPConnection.LDAP_V3;            String ldapHost       = args[0];            String loginDN        = args[1];            String password       = args[2];            String containerName  = args[3];            LDAPConnection lc = new LDAPConnection();            LDAPAttribute  attribute = null;            LDAPAttributeSet attributeSet = new LDAPAttributeSet();            /* To Add an entry to the directory,          *   -- Create the attributes of the entry and add them to an attribute set          *   -- Specify the DN of the entry to be created          *   -- Create an LDAPEntry object with the DN and the attribute set          *   -- Call the LDAPConnection add method to add it to the directory          */                      String objectclass_values[] = { "inetOrgPerson" };            attribute = new LDAPAttribute( "objectclass", objectclass_values );            attributeSet.add( attribute );                 String cn_values[] = { "James Smith", "Jim Smith", "Jimmy Smith" };            attribute = new LDAPAttribute( "cn", cn_values );            attributeSet.add( attribute );            String givenname_values[] = { "James", "Jim", "Jimmy" };            attribute = new LDAPAttribute( "givenname", givenname_values );            attributeSet.add( attribute );            attributeSet.add( new LDAPAttribute( "sn", "Smith" ) );            attributeSet.add( new LDAPAttribute( "telephonenumber",                                                         "1 801 555 1212" ) );            attributeSet.add( new LDAPAttribute( "mail", "JSmith@Acme.com" ) );            String  dn  = "cn=JSmith," + containerName;                 LDAPEntry newEntry = new LDAPEntry( dn, attributeSet );            try {                // connect to the server                lc.connect( ldapHost, ldapPort );                // authenticate to the server                lc.bind( ldapVersion, loginDN, password );                lc.add( newEntry );                System.out.println( "\nAdded object: " + dn + " successfully." );                // disconnect with the server                lc.disconnect();            }            catch( LDAPException e ) {                System.out.println( "Error:  " + e.toString());            }                                              System.exit(0);        }    }