2007-11-28

LDAP应用技术简述 转载

关键字: ldap

LDAP应用技术简述

编写LDAP访问程序; a)        JAVA                         i.              JNDI(JAVA 命名及目录接口)

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

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

经常使用的LDAP驱动有JDK自带的LDAP provider,还有IBMPROVIDER,以及NOVELJNDI产品。需要提醒的是,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开发的,原是针对NovelNDS目录设计的JAVA访问工具。NOVELNDS和网景(NETSCAPE)的目录是工具界最早的目录产品。JLDAP并非JNDI的服务供应者,而是同一抽象层次下的访问工具集。与JNDI-LDAP相比,JLDAP更接近于类关系数据库的访问方式。

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

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

例:

java 代码
  1. import com.novell.ldap.*;  
  2.   
  3. public class AddEntry  
  4.   
  5. {  
  6.   
  7.     public static void main( String[] args )  
  8.   
  9.     {  
  10.   
  11.         if (args.length != 4) {  
  12.   
  13.             System.err.println("Usage:   java AddEntry   
  14.   
  15.                                                 + "   " );  
  16.   
  17.             System.err.println("Example: java AddEntry Acme.com"  
  18.   
  19.                         + " \"cn=admin,o=Acme\" secret \"ou=Sales,o=Acme\"");  
  20.   
  21.             System.exit(1);  
  22.   
  23.         }  
  24.   
  25.                  
  26.   
  27.         int ldapPort = LDAPConnection.DEFAULT_PORT;  
  28.   
  29.         int ldapVersion  = LDAPConnection.LDAP_V3;  
  30.   
  31.         String ldapHost       = args[0];  
  32.   
  33.         String loginDN        = args[1];  
  34.   
  35.         String password       = args[2];  
  36.   
  37.         String containerName  = args[3];  
  38.   
  39.         LDAPConnection lc = new LDAPConnection();  
  40.   
  41.         LDAPAttribute  attribute = null;  
  42.   
  43.         LDAPAttributeSet attributeSet = new LDAPAttributeSet();  
  44.   
  45.         /* To Add an entry to the directory, 
  46.          *   -- Create the attributes of the entry and add them to an attribute set 
  47.          *   -- Specify the DN of the entry to be created 
  48.          *   -- Create an LDAPEntry object with the DN and the attribute set 
  49.          *   -- Call the LDAPConnection add method to add it to the directory 
  50.          */            
  51.   
  52.         String objectclass_values[] = { "inetOrgPerson" };  
  53.   
  54.         attribute = new LDAPAttribute( "objectclass", objectclass_values );  
  55.   
  56.         attributeSet.add( attribute );       
  57.   
  58.         String cn_values[] = { "James Smith""Jim Smith""Jimmy Smith" };  
  59.   
  60.         attribute = new LDAPAttribute( "cn", cn_values );  
  61.   
  62.         attributeSet.add( attribute );  
  63.   
  64.         String givenname_values[] = { "James""Jim""Jimmy" };  
  65.   
  66.         attribute = new LDAPAttribute( "givenname", givenname_values );  
  67.   
  68.         attributeSet.add( attribute );  
  69.   
  70.         attributeSet.add( new LDAPAttribute( "sn""Smith" ) );  
  71.   
  72.         attributeSet.add( new LDAPAttribute( "telephonenumber",  
  73.   
  74.                                                      "1 801 555 1212" ) );  
  75.   
  76.         attributeSet.add( new LDAPAttribute( "mail""JSmith@Acme.com" ) );  
  77.   
  78.         String  dn  = "cn=JSmith," + containerName;       
  79.   
  80.         LDAPEntry newEntry = new LDAPEntry( dn, attributeSet );  
  81.   
  82.         try {  
  83.   
  84.             // connect to the server  
  85.   
  86.             lc.connect( ldapHost, ldapPort );  
  87.   
  88.             // authenticate to the server  
  89.   
  90.             lc.bind( ldapVersion, loginDN, password );  
  91.   
  92.             lc.add( newEntry );  
  93.   
  94.             System.out.println( "\nAdded object: " + dn + " successfully." );  
  95.   
  96.             // disconnect with the server  
  97.   
  98.             lc.disconnect();  
  99.   
  100.         }  
  101.   
  102.         catch( LDAPException e ) {  
  103.   
  104.             System.out.println( "Error:  " + e.toString());  
  105.   
  106.         }                                    
  107.   
  108.         System.exit(0);  
  109.   
  110.     }  
  111.   
  112. }  

评论
发表评论

您还没有登录,请登录后发表评论

jacally
搜索本博客
我的相册
5225c084-f830-3bfb-89b4-e391c07b2221-thumb
flexlib6-1.jpg
共 27 张
最近加入圈子
存档
最新评论