今天終於把添加,刪除,修改節點名,屬性,遍曆節點都弄出來了,先把代碼貼出來吧
/**
*
* @author chenyi
*/
import java.util.Hashtable;
import javax.naming.directory.*;
import java.util.*;
import javax.naming.*;
public class ChenYi {
DirContext dc = null;
String account = "Manager";//操作LDAP的帳戶。預設就是Manager。
String password = "secret";//帳戶Manager的密碼。
String root = "dc=example,dc=com"; //LDAP的根節點的DC
public ChenYi() {
init();
//add();//添加節點
//delete("ou=hi,dc=example,dc=com");//刪除"ou=hi,dc=example,dc=com"節點
//modifyInformation("ou=hi,dc=example,dc=com");//修改"ou=hi,dc=example,dc=com"屬性
//renameEntry("ou=new,o=neworganization,dc=example,dc=com","ou=neworganizationalUnit,o=neworganization,dc=example,dc=com");//重新命名節點"ou=new,o=neworganization,dc=example,dc=com"
searchInformation("dc=example,dc=com", "", "(objectclass=*)");//遍曆所有根節點
//searchInformation("o=neworganization,dc=example,dc=com","","(objectclass=*)");//遍曆指定節點的分節點
close();
}
public void init() {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.100.221:389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
dc = new InitialDirContext(env);//初始化上下文
System.out.println("認證成功");//這裡可以改成異常拋出。
} catch (javax.naming.AuthenticationException e) {
System.out.println("認證失敗");
} catch (Exception e) {
System.out.println("認證出錯:" + e);
}
}
public void close() {
if (dc != null) {
try {
dc.close();
} catch (NamingException e) {
System.out.println("NamingException in close():" + e);
}
}
}
public void add() {
try {
String newUserName = "hi";
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectClass");
objclassSet.add("top");
objclassSet.add("organizationalUnit");
attrs.put(objclassSet);
attrs.put("ou", newUserName);
dc.createSubcontext("ou=" + newUserName + "," + root, attrs);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception in add():" + e);
}
}
public void delete(String dn) {
try {
dc.destroySubcontext(dn);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception in delete():" + e);
}
}
public boolean modifyInformation(String dn) {
try {
ModificationItem[] mods = new ModificationItem[1];
/*添加屬性*/
// Attribute attr0 = new BasicAttribute("description",
// "測試");
// mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,attr0);
/*修改屬性*/
// Attribute attr0 = new BasicAttribute("description", "陳軼");
// mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
// attr0);
/*刪除屬性*/
Attribute attr0 = new BasicAttribute("description",
"陳軼");
mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
attr0);
dc.modifyAttributes(dn, mods);
return true;
} catch (NamingException ne) {
ne.printStackTrace();
System.err.println("Error: " + ne.getMessage());
return false;
}
}
/**
* @param base :根節點(在這裡是"dc=example,dc=com")
* @param scope :搜尋範圍,分為"base"(本節點),"one"(單層),""(遍曆)
* @param filter :指定子節點(格式為"(objectclass=*)",*是指全部,你也可以指定某一特定類型的樹節點)
*/
public void searchInformation(String base, String scope, String filter) {
SearchControls sc = new SearchControls();
if (scope.equals("base")) {
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
} else if (scope.equals("one")) {
sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} else {
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
}
NamingEnumeration ne = null;
try {
ne = dc.search(base, filter, sc);
// Use the NamingEnumeration object to cycle through
// the result set.
while (ne.hasMore()) {
System.out.println();
SearchResult sr = (SearchResult) ne.next();
String name = sr.getName();
if (base != null && !base.equals("")) {
System.out.println("entry: " + name + "," + base);
} else {
System.out.println("entry: " + name);
}
Attributes at = sr.getAttributes();
NamingEnumeration ane = at.getAll();
while (ane.hasMore()) {
Attribute attr = (Attribute) ane.next();
String attrType = attr.getID();
NamingEnumeration values = attr.getAll();
Vector vals = new Vector();
// Another NamingEnumeration object, this time
// to iterate through attribute values.
while (values.hasMore()) {
Object oneVal = values.nextElement();
if (oneVal instanceof String) {
System.out.println(attrType + ": " + (String) oneVal);
} else {
System.out.println(attrType + ": " + new String((byte[]) oneVal));
}
}
}
}
} catch (Exception nex) {
System.err.println("Error: " + nex.getMessage());
nex.printStackTrace();
}
}
public boolean renameEntry(String oldDN, String newDN) {
try {
dc.rename(oldDN, newDN);
return true;
} catch (NamingException ne) {
System.err.println("Error: " + ne.getMessage());
return false;
}
}
public static void main(String[] args) {
new ChenYi();
}
}