JNDI目录服务安装环境配置

JNDI目录服务入门教程
本教程讲述下面内容:
1.    iPlanet Directory Server5.1目录服务器
iPlanet Directory Server5.1的下载
iPlanet Directory Server5.1的安装
iPlanet Directory5.1的启动,关闭
iPlanet Directory5.1的管理员指南

2.    安装编程环境所需的类库
JNDI类库下载
LDAP类库下载
3.    一些基本的目录服务操作
目录服务器建立连接(匿名访问方式)
目录服务器建立连接(简单验证方式Simple)
 获取目录服务器指定条目的属性值
 按条目属性值的搜索条件搜索目录服务器上的条目
 删除,添加,修改目录服务器指定条目的属性值
 删除,添加目录服务器上的条目,重命名目录服务器上的条目识别名(DN)

1 iPlanet Directory Server5.1目录服务器简述
iPlanet Directory Server 产品包括管理多个目录的 iPlanet Directory Server 和通过图形界面管理这两个服务器的 iPlanet Console。本章概述有关 iPlanetDirectory Server 的信息以及使用控制台建立目录管理服务所需的最基本的任务。
iPlanet Directory Server 的运行平台包括:widnows 和 Linux
iPlanet Directory Server 目前最高版本为V5.2

2 iPlanet Directory Server5.1的下载
下载网址:http://www.iplanet.com/
目前已更新为http://wwws.sun.com/software/

3 iPlanet Directory Server5.1的安装
以下为iPlanet Directory Server5.1的安装步骤
步骤1
启动iPlanet Directory Server5.1的安装界面
欢迎界面及软件许可界面
在出现的界面中,单击“下一步”按钮
1.gif

步骤2
许可协议界面
在出现的界面单击“是”
2.gif

步骤3
选择安装的组件,包装两个安装类型
iPlanet服务器软件
包括iPlanet目录服务器,iPlanet控制台及其它相关组件
iPlanet控制台
只安装iPlanet控制台,用于安装在客户端
出现选择安装类型时,选择Iplanet 服务器软件
3.gif

步骤4
选择安装类型
在出现的界面选择“典型”安装
4.gif

步骤5
选择安装位置
出现的界面选择安装位置
5.gif

步骤6
选择安装组件
出现的界面选择你所需要安装的组件,我们所有的都安装
6.gif

步骤7
配置目录服务器
建一个新的目录服务器
使用现有的目录服备器
7.gif

步骤8
目录数据库的存储选择
建一个新的目录数据库
使用现有的目录数据库
8.gif

步骤9
填写目录服务器的标识符,连接端口号,及后缀
9.gif

步骤10
目录服务器控制台的管理员ID及口令
10.gif

步骤11
设置目录服务器的管理域
11.gif

步骤12
设置连接目录服务器的目录管理员识别名(DN)
及口令
12.gif

步骤13
设置管理目录服务器服务的端口号
13.gif

步骤14
显示以上的用户选项信息
14.gif

步骤15
安装进程
15.gif

步骤16
注意,安装后,需重启计算机服务才有效.
16.gif


iPlanet Directory5.1管理控制台的启动,关闭
单击命令%iPlanet%/Servers/start-admin.cmd
启动管理进程
单击命令%iPlanet%/Servers/ startconsole.exe
启动管理控制台
A1.gif

     
4 iPlanet Directory5.1的管理员指南
  关于这个指南,你可以到网上找或给我发邮件(moaihe1111@163.com),我可以发给你哦。

5 安装编程环境类库
基本编程环境类库
只需安装J2EE1.3版以上即可,下载网址http://java.sun.com
JNDI类库下载
下载类库jndi.jar 
下载网址:http://java.sun.com/jndi/
LDAP类库下载
下载类库jaas.jar, ldap.jar, ldapbp.jar, ldapsec.jar, providerutil.jar
下载网址:http://java.sun.com/jndi/

6 一些基本的目录服务操作
 1 目录服务器建立连接( 匿名访问方式)
 程序段:
     /*实例化一个Hashtable,用于存储连接属性*/
     Hashtable env = new Hashtable(5, 0.75f);
   /*设置连接Ldap的实现工厂*/
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  /*设置连接URL*/
env.put(Context.PROVIDER_URL,“ldap://localhost:389”);
     /*实例化一个目录上下文*/
    LdapContext ctx = new InitialLdapContext(env, null);

 2 目录服务器建立连接(简单验证方式Simple)
程序段:
   /*实例化一个Hashtable,用于存储连接属性*/
     Hashtable env = new Hashtable(5, 0.75f);
   /*设置连接Ldap的实现工厂*/
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  /*设置连接URL*/
env.put(Context.PROVIDER_URL,“ldap://localhost:389”);
/*设置验证方式为简单验证simple*/
env.put(Context.SECURITY_AUTHENTICATION, "simple");
/*进入的目录识别名DN
Env.MGR_DN为进入的目录识别名*/
    env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
/*进入的目录密码password
Env.MGR_PW为进入的目录密码*/
    env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);
/*实例化一个目录上下文*/
    LdapContext ctx = new InitialLdapContext(env, null);

 3 获取目录服务器指定条目的属性值
/*定义需获取的属性名*/

String[] attrs = new String[5];
        attrs[ 0 ] = "cn";              /* 获取属性”cn” */
        attrs[ 1 ] = "sn";              /* 获取属性”sn” */
        attrs[ 2 ] = "mail";            /* 获取属性”mail” */
        attrs[ 3 ] = "telephonenumber"; /* 获取属性”telephonenumber” */
        attrs[ 4 ] ="title";            /* 获取属性”title” */
/*获取指定条目*/
        Attributes result = ctx.getAttributes(Env.ENTRYDN, attrs);
/*获取指定属性*/
Attribute attr = result.get("cn");

 按条目属性值的搜索条件搜索目录服务器上的条目
 /*实例化一个搜索器*/
 SearchControls constraints = new SearchControls();
/*设置搜索器的搜索范围*/
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

        /* 在基目录中搜索条件为Env.MY_FILTER的所有属性
Env.MY_SEARCHBASE为搜索的基目录
Env.MY_FILTER为搜索的过渡器
注意:这里返回是的所有的条目集合*/
        NamingEnumeration results
            = ctx.search(Env.MY_SEARCHBASE, Env.MY_FILTER, constraints);

        /* 打印条目的识别名(DN)及其所有的属性名,值*/
        while (results != null && results.hasMore())
 {
  /*取一个条目*/
            SearchResult si = (SearchResult)results.next();

            /* 打印条目识别名(DN) */
            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.println(attrId + ": " + vals.nextElement()))
                            ;
                }
            }
 4 删除,添加,修改目录服务器指定条目的属性值
 /* 实例化一个用于修改属性的数组*/
        ModificationItem[] mods = new ModificationItem[3];
/*实例化一个BasicAttribute基本属性对象
其第一个参数为属性名,
第二个参数为属性值*/
        Attribute mod0 = new BasicAttribute("mail", "babs@eng");
        // 定义一个修改的属性条
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);

 /*实例化一个BasicAttribute基本属性对象
其第一个参数为属性名,
第二个参数为属性值*/
        Attribute mod1 =
            new BasicAttribute("description",
                          "This entry was modified with the Modattrs program on " +
                          (new Date()).toString());
    // 定义一个增加的属性条
        mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);

 /*实例化一个BasicAttribute基本属性对象
其第一个参数为属性名/

        Attribute mod2 =  new BasicAttribute("description");
      // 定义一个删除的属性条
        mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, mod2);


        /* 执行操作 */
        ctx.modifyAttributes(Env.ENTRYDN, mods);

 5 删除,添加目录服务器上的条目,重命名目录服务器上的条目识别名(DN)
    /* 建一个我们将要增加的条目的属性*/
Attribute objClasses = new BasicAttribute("objectclass");
/*以下给属性增加属性值
注意:这里是一个属性对应多个属性值*/
    objClasses.add("top");  /*给属性objectclass增加属性值”top”*/
    objClasses.add("person"); /*给属性objectclass增加属性值”person”*/
    objClasses.add("organizationalPerson"); /*给属性objectclass增加属值”organizationalPerson”*/
    objClasses.add("inetOrgPerson"); /*给属性objectclass增加属性值”inetOrgPerson“*/

/*v建立一个属性,其属性名为”cn”,值为”Jacques Smith”*/
Attribute cn = new BasicAttribute("cn", "Jacques Smith");
/*v建立一个属性,其属性名为”sn”,值为”Smith”*/
Attribute sn = new BasicAttribute("sn", "Smith");
/*v建立一个属性,其属性名为”givenname”,值为”Jacques”*/
    Attribute givenNames = new BasicAttribute("givenname", "Jacques");

    /* 指定一个我们将要增加DN*/
    String dn = "uid=liaowufeng," + Env.MY_MODBASE;
    /*指定一个我们将要删除的DN */
    String ndn = "uid=Jacques Smith," + Env.MY_MODBASE;
    /* 指定一个DN名,我们将用于给DN更名 */
    String nrdn = "uid=zhengxianagyang";
    /*实例化一个Hashtable,用于存储连接属性*/
Hashtable env = new Hashtable(5, 0.75f);

    /*设置连接Ldap的实现工厂*/
    env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);

    
 /*设置连接URL*/
    env.put(Context.PROVIDER_URL, Env.MY_SERVICE);

    
/*设置验证方式为简单验证simple*/
env.put(Context.SECURITY_AUTHENTICATION, "simple");
/*进入的目录识别名DN
env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
/*进入的目录密码password
    env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);
    /*实例化一个目录上下文*/
    DirContext ctx = null;

    try {
        /*/*实例化一个目录上下文*/*/
        ctx = new InitialDirContext(env);
       /*实例化一个属性集合*/
        Attributes orig = new BasicAttributes();
        /*装入属性*/
orig.put(objClasses);
orig.put(cn);
        orig.put(sn);
        orig.put(givenNames);

        /* 在识别名为DN的目录中增加一个条目*/
        ctx.createSubcontext(dn, orig);

        System.out.println( "Added entry " + dn + ".");

    } catch (NameAlreadyBoundException e) {
        /* 如果条目存在,则触发此Exception */
        System.out.println("Entry " + dn + " already exists, no need to add");
    } catch (NamingException e) {
        System.err.println("Modrdn: problem adding entry." + e);
        System.exit(1);
    }


    try {
     /*删除一个条目ndn*/
      ctx.destroySubcontext(ndn);

      System.out.println("Deleted entry " + ndn + ".");

    }
catch (NameNotFoundException e) {
 /*若要删除的条目不存在,则触发此Exception*/
      System.out.println("Entry " + ndn + " is not in the directory.  " +
                         "No need to delete.");
    }
    catch (NamingException e) {
      System.err.println("Modrdn: problem deleting entry." + e);
      System.exit(1);
    }


    /* 更改条目DN(识别名)操作*/

try {
    /*将条目dn,更名为nrdn*/
        ctx.rename(dn, nrdn);
        System.out.println("The modrdn operation was successful.  Entry " +
                           dn + " has been changed to " + nrdn + ".");
} catch (NamingException e) {
  /*操作失败,则触发此意外*/
        System.err.println("Modify operation failed." + e);
    }
}

原文地址:https://www.cnblogs.com/liaomin416100569/p/9331927.html