Ldap
采用的是novell 的API。
参考代码如下:
创建连接LDAP的类:Ldap_Connection
package ldap1;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
public class Ldap_Connection {
@SuppressWarnings("deprecation")
public static LDAPConnection getLdapConnection(){
LDAPConnection lc = new LDAPConnection();
try {
lc.connect("192.168.1.13",9389); lc.bind(LDAPConnection.LDAP_V3,"cn=li,ou=People,dc=gsoft,dc=com","newpassword");
System.out.println("连接成功!");
return lc ;
} catch (LDAPException e) {
System.err.print("连接异常! ");
e.printStackTrace();
}
return null;
}
}
创建添加类:Ldap_Add
package ldap1;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
public class Ldap_Add {
public static void LDAP_add(LDAPConnection ldapConnection) throws LDAPException{
LDAPAttributeSet attributeSet = new LDAPAttributeSet();
attributeSet.add(new LDAPAttribute("objectclass", new String(
"inetOrgPerson")));
attributeSet.add(new LDAPAttribute("cn", new String[] { "li",
"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=li,ou=People,dc=gsoft,dc=com",
attributeSet);
ldapConnection.add(entry);
System.out.println("成功的添加了一条记录!");
ldapConnection.disconnect();
}
}
创建删除类:Ldap_Delete
package ldap1;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
public class Ldap_Delete {
public static void LDAP_Delete(LDAPConnection lc) throws LDAPException{
LDAPConnection lConnection = Ldap_Connection.getLdapConnection() ;
lConnection.delete("cn=李,ou=People,dc=gsoft,dc=com") ;
lConnection.disconnect() ;
System.out.println("删除一条记录");
}
}
创建查询类:Ldap_Seach
package ldap1;
import java.util.Iterator;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchResults;
/**
* LDAPConnection.SCOPE_ONE // 只查找基节点第一层的子节点
* LDAPConnection.SCOPE_BASE // 只查找基节点
* LDAPConnection.SCOPE_SUB // 查找基节点下面的所有子节点
**/
public class Ldap_Search {
public static void LDAP_Search(LDAPConnection lc) throws LDAPException{
LDAPSearchResults rs = lc.search("dc=gsoft,dc=com",LDAPConnection.SCOPE_SUB,"objectClass=*",null,false);
int count = 0;
while(rs.hasMore()){
LDAPEntry entry = rs.next();
System.out.println(entry.getDN());
count++;
}
lc.disconnect() ;
System.out.println("共有"+count+"条记录。");
lc.disconnect();
}
/**
* 遍历该用户下的所有属性和属性值
* **/
public static void Ldap_Search2(LDAPConnection lc) throws LDAPException{
//http://hi.baidu.com/maimouse/blog/item/5862142c746249e88b139910.html
//http://blog.csdn.net/ZhuYujie/archive/2008/07/21/2685366.aspx
LDAPSearchResults rs = lc.search("cn=li,ou=People,dc=gsoft,dc=com",LDAPConnection.SCOPE_SUB,"objectClass=*",null,false);
int count = 0;
Iterator iterator = null ;
LDAPEntry entry ;
while(rs.hasMore()){
entry = rs.next();
// System.out.println(entry.getDN());
LDAPAttributeSet lAttributeSet = entry.getAttributeSet();
iterator = (Iterator) lAttributeSet.iterator();
while(iterator.hasNext()){
LDAPAttribute Attribute =(LDAPAttribute) iterator.next() ;
System.out.println("name="+Attribute.getName());
System.out.println("vale="+Attribute.getStringValue());
count++;
}
}
lc.disconnect() ;
System.out.println("共有"+count+"条记录。");
lc.disconnect();
}
}
创建修改类:Ldap_Search
package ldap1;
import java.util.ArrayList;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPModification;
public class Ldap_Update {
public static void LDAP_Upate(LDAPConnection lc) throws LDAPException{
ArrayList<LDAPModification> modList = new ArrayList<LDAPModification>();
LDAPAttribute attribute = new LDAPAttribute("mail", "
[email protected]");
modList.add(new LDAPModification(LDAPModification.REPLACE, attribute));
// attribute = new LDAPAttribute("givenName","修改") ;
// 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连接就行修改操作
String dn = "cn=li,ou=People,dc=gsoft,dc=com" ;
lc.modify(dn, modsadd);
lc.disconnect() ;
}
}
创建测试类:Ldap_Main
package ldap1.main;
import ldap1.Ldap_Add;
import ldap1.Ldap_Connection;
import ldap1.Ldap_Delete;
import ldap1.Ldap_Search;
import ldap1.Ldap_Update;
import com.novell.ldap.LDAPConnection;
//http://blog.csdn.net/TechChan/archive/2010/04/13/5482170.aspx
public class Ldap_Main {
public static void main(String[] args) {
LDAPConnection ldapConnection = Ldap_Connection.getLdapConnection() ;
try {
Ldap_Delete.LDAP_Delete(ldapConnection);
Ldap_Add.LDAP_add(ldapConnection) ;
Ldap_Search.LDAP_Search(ldapConnection) ;
Ldap_Update.LDAP_Upate(ldapConnection) ;
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上几个简单的功能,能够完成对Ldap的连接,添加、删除、更新、查询的简单操作。