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 );           }      }  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值