<一>
<二>
下面来具体的说一下怎么用JLDAP。首先要去下载一下JLDAP,具体下载的地址可以上网去搜。下载下来以后,lib里面的是开发所要用到的包,doc里面是帮助文档API和示例程序。
LDAPConnection lc = new LDAPConnection(); try { lc.connect("6.1.19.154",389); lc.bind(LDAPConnection.LDAP_V3,"cn=xxx","xxxxxx"); LDAPSearchResults rs = lc.search("dc=excel,dc=com,dc=cn",LDAPConnection.SCOPE_SUB,"objectClass=*",null,false); int count = 0; while(rs.hasMore()){ LDAPEntry entry = rs.next(); System.out.println(entry.getDN()); count++; } System.out.println("共有"+count+"条记录。"); } catch (LDAPException e) { System.err.print("连接异常! "); e.printStackTrace(); }
<三>
虽然LDAP主要是用来进行读操作的,但不可避免的,我们也要向其中添加一些新的数据。用JLDAP向LDAP服务器添加数据的操作也是非常简单的。
LDAPConnection con = new LDAPConnection(); con.connect("hostname",hostport); con.bind("version","DN","password");
LDAPAttributeSet attributeSet = new LDAPAttributeSet(); attributeSet.add(new LDAPAttribute("objectclass", new String("inetOrgPerson"))); attributeSet.add(new LDAPAttribute("cn", new String[] { "李", "Jim Smith", "Jimmy Smith" })); attributeSet.add(new LDAPAttribute("givenname", new String[] { "测试", "Jim", "Jimmy" })); attributeSet.add(new LDAPAttribute("sn", new String("Smith"))); attributeSet.add(new LDAPAttribute("telephonenumber", new String("1 801 555 1212"))); attributeSet.add(new LDAPAttribute("mail", new String("[email protected]"))); attributeSet.add(new LDAPAttribute("userpassword", new String("newpassword"))); LDAPEntry entry = new LDAPEntry("cn=李,cn=Lizl,dc=excel,dc=com,dc=cn", attributeSet); LDAPConnection con = new LDAPConnection(); con.connect("6.1.19.154", 389); con.bind(LDAPConnection.LDAP_V3, "cn=XXX", "XXXXXX"); con.add(entry); System.out.println("成功的添加了一条记录!"); con.disconnect();
<四>
这里来说一说怎么从LDAP中删除一个实体。
LDAPConnection con = new LDAPConnection(); con.connect("6.1.19.154",389); con.bind(LDAPConnection.LDAP_V3,"cn=XXXX","XXXXXX"); con.delete("cn=JSmith,dc=excel,dc=com,dc=cn"); System.out.println("成功删除一条记录!");