`
zl198751
  • 浏览: 272896 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

对于NOVELL LDAP 轻量级目录服务的学习理解

    博客分类:
  • LDAP
阅读更多

对于NOVELL LDAP 轻量级目录服务的学习理解
采用NOVELL 的API
用到得包如下

import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;


如何获取到一个LDAP服务器的连接

   首先要知道
    服务器地址 ldapHost,
    服务器端口 ldapPort, 默认为 LDAPConnection.DEFAULT_PORT|389
    服务器版本 ldapVersion, 默认为 LDAPConnection.LDAP_V3|3
    服务器管理员 DN,
    服务器密码 password,
   第一步
   LDAPConnection lc = new LDAPConnection();
   lc.connect(ldapHost,ldapPort);
   lc.bind(ldapVersion, username, password.getBytes("UTF8"));
   如果没抛异常,lc即为到LDAPConnection的连接
   
   在操作完成之后一定要关闭LDAP连接 lc.disconnect();

如何在LDAP服务器中加入一个实体即com.novell.ldap.LDAPEntry;

   定义LDAPAttributeSet attributeSet = new LDAPAttributeSet();
   在attributeSet 中加入属性 注意各种实体参数正确 比如 objectclass 具体说明见:ldap 协议 objectclasses 与 attribute
    attributeSet.add(new LDAPAttribute("objectclass", "inetOrgPerson"));
    attributeSet.add(new LDAPAttribute("riseGUID", riseGUID.toString()));
    attributeSet.add(new LDAPAttribute("fullName", name));
    attributeSet.add(new LDAPAttribute("userpassword", password));
    attributeSet.add(new LDAPAttribute("sn", name));
   确定实体dn 即实体在 LDAP 上的树形结构中的位置
   建立实体对象
   LDAPEntry newEntry = new LDAPEntry(dn, attributeSet);
   通过 LDAPConnection 连接 添加此实体 
   lc.add(newEntry);

如何在LDAP服务器中获取某实体

   第一、获取到LDAP服务器的管理员连接
   第二、确定查找参数 包括:该实体的DN 、String returnAttrs[] = { "LoginDisabled", 
      "loginExpirationTime","passwordExpirationTime","loginAllowedTimeMap","lockedByIntruder" };
     //字符串数组 表示要查找的参数 类似数据库中要查找的数据表字段名
   第三、验证某实体的属性
   LDAPAttribute attribute = new LDAPAttribute("userPassword", userPWD);
   lc.compare(userDN, attribute)   //返回true or false 可以用来判断用户的密码是否正确
   第四、进行查找
    LDAPEntry entry = lc.read(userDN, returnAttrs); //返回的是 LDAPEntry 对象 通过 LDAPEntry 对象来获取该实体的信息
如何在LDAP服务器中查找实体 
   第一、获取到LDAP服务器的管理员连接
   第二、确定查找范围,查找参数,查找内容等
    如:String searchBase = "o=武钢氧气有限责任公司";
     String searchFilter = "(cn=a000001)";        // 此处为查找表达式,支持正则表达式
     String searchScope = LDAPConnection.SCOPE_ONE  // 只查找基节点第一层的子节点
           |LDAPConnection.SCOPE_BASE // 只查找基节点
           |LDAPConnection.SCOPE_SUB  // 查找基节点下面的所有子节点
   第三、根据参数进行查找 返回 LDAPSearchResults 对象
   LDAPSearchResults searchResults = lc.search(searchBase,
     searchScope, searchFilter, new String[] { "cn",
       "objectClass", "userPassword","riseGUID"},false);
                  // 此方法中字符串数组表示查询的实体的属性,并在结果中返回这些属性
   第四:列出查询结果
   
    while (searchResults.hasMore()) {        //LDAPSearchResults 实现了 collection 接口
    LDAPEntry le = searchResults.next();      //结果集中每个内容都是一个 LDAPEntry 对象
    System.out.println(le.getDN());
    LDAPAttributeSet attributeSet = le.getAttributeSet();  //通过 LDAPEntry 对象来获取 LDAPAttributeSet 对象
    Set sortedAttributes = new TreeSet(attributeSet);
    Iterator allAttributes = sortedAttributes.iterator();

    while (allAttributes.hasNext()) {
     LDAPAttribute attribute = (LDAPAttribute) allAttributes.next();
     String attributeName = attribute.getName();   //获取参数名
     System.out.println("\t\t" + attributeName);   
     Enumeration allValues = attribute.getStringValues();//其参数值可以为多个,利用Enumeration 列出全部该属性的值
     if (allValues != null) {
      while (allValues.hasMoreElements()) {
       String Value = (String) allValues.nextElement();
       System.out.println("\t\t\t" + Value);
      }
     }
    }
   }
如何修改、删除 已存在的某实体的 属性 Attribute
    第一、获取到LDAP服务器的管理员连接
    第二、创建该实体要修改的属性列表 ArrayList modList = new ArrayList();
    第三、用 LDAPModification 对象填充该列表 如:
      LDAPAttribute attribute = new LDAPAttribute("telephoneNumber", "1 801 555 1212");
      modList.add(new LDAPModification(LDAPModification.ADD, attribute));
      // 这里 LDAPModification 对象有几种方式 LDAPModification.ADD、LDAPModification.DELETE、LDAPModification.REPLACE
    第四、用list填充 LDAPModification  对象
      LDAPModification[] modsadd = new LDAPModification[modList.size()];
      modsadd = (LDAPModification[]) modList.toArray(modsadd);
    第五、通过 LDAP连接就行修改操作
      lc.modify(dn, modsadd);         //注:lc为LDAP服务器的连接 modsadd 为封装后的LDAPModification 对象
      
      
如何删除某个已存在实体
    通过获取LDAP连接 deleteDN为该节点的DN
    lc.delete(deleteDN);
    
    
    
    
JLDAP 访问LDAP服务器的错误代码含义(已知的)

91:Unable to connect to server 172.16.5.12:389 (91) Connect Error  //在获取连接的时候LDAP 服务器连接地址或端口不正确
68:Entry Already Exists (68) Entry Already Exists      //添加实体时抛出实体已经存在信息
32:No Such Object (32) No Such Object        //实体不存在

分享到:
评论

相关推荐

    TCP/IP教程TCP/IP基础

    第16章 LDAP:目录服务 153 16.1 为什么使用目录服务 153 16.2 目录服务的功能 153 16.3 IP上的目录服务 154 16.4 OSI X.500目录模型 156 16.4.1 早期的X.500 157 16.4.2 今天的X.500 157 16.5 LDAP结构 157 16.5.1 ...

    rfc中文文档目录,包含部分翻译

    RFC1777_轻量级目录访问协议 RFC1787_在多供应Internet上的软件路由 RFC1796_不是所有RFCs是标准 RFC1797_A级子网实验 RFC1810_报告MD5性能 RFC1818_最好最新的实践 RFC1822 使用具备Photuris技术的指定IBM专利的...

    TCPIP协议详解(4-1)

    LDAP:目录服务 153 16.1 为什么使用目录服务 153 16.2 目录服务的功能 153 16.3 IP上的目录服务 154 16.4 OSI X.500目录模型 156 16.4.1 早期的X.500 157 16.4.2 今天的X.500 157 16.5 LDAP...

    TCP-IP技术大全

    LDAP:目录服务 153 16.1 为什么使用目录服务 153 16.2 目录服务的功能 153 16.3 IP上的目录服务 154 16.4 OSI X.500目录模型 156 16.4.1 早期的X.500 157 16.4.2 今天的X.500 157 16.5 LDAP...

    中文版RFC,共456

    RFC1777 轻量级目录访问协议 RFC1787 在多供应Internet上的软件路由 RFC1796 不是所有RFCs是标准 RFC1797 A级子网实验 RFC1810 报告MD5性能 RFC1818 最好最新的实践 RFC1822 使用具备Photuris技术的指定IBM专利的...

    RFC中文文档-txt

    RFC1777 轻量级目录访问协议 RFC1787 在多供应Internet上的软件路由 RFC1796 不是所有RFCs是标准 RFC1797 A级子网实验 RFC1810 报告MD5性能 RFC1818 最好最新的实践 RFC1822 使用具备Photuris技术的指定IBM专利的...

    TCP/IP技术大全(中文PDF非扫描版)

    包括开放式通信模型、TCP/IP通信模型、IP网络中的命名和寻址机制、地址解析及反向地址解析协议、DNS域字服务器、WINS、地址发现协议、IPv6、IP网络中的路由协议(RIP、OSPF等)、互联网打印协议、LDAP目录服务、远程...

    TCP/IP技术大全

    第16章 LDAP:目录服务 153 16.1 为什么使用目录服务 153 16.2 目录服务的功能 153 16.3 IP上的目录服务 154 16.4 OSI X.500目录模型 156 16.4.1 早期的X.500 157 16.4.2 今天的X.500 157 16.5 LDAP结构 157 16.5.1 ...

    TCP/IP详解

    第16章 LDAP:目录服务 153 16.1 为什么使用目录服务 153 16.2 目录服务的功能 153 16.3 IP上的目录服务 154 16.4 OSI X.500目录模型 156 16.4.1 早期的X.500 157 16.4.2 今天的X.500 157 16.5 LDAP结构 157 16.5.1 ...

Global site tag (gtag.js) - Google Analytics