jndi操作Ldap進行增刪改查


由於是第一次操作Ldap 網上各種查資料,然後自己總結了一點

首先要獲取Ldap連接

連接Ldap做操作需要一些必要的參數 其實跟jdbc連接數據庫差不多 下面是返回一個ldap連接 (由於我的需求是做同步數據,連接的URL之類的屬性都是不一樣的,也都是從配置文件中讀取,所以增刪改查的方法都用到了獲取連接的方法)

/**
* 連接LDAP
* @param uLdapURL連接URL
* @param searchDN需要查詢的節點DN
* @return Ldap連接
*/
public static LdapContext ConnLdap(String uLdapURL, String username,
String password) {
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, uLdapURL);     
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
LdapContext ctx = null;
try {
ctx = new InitialLdapContext(env, null);
} catch (NamingException e) {
System.out.println("Lookup failed: " + e);
}
return ctx;
}

/**
 * 根據指定的節點查詢
* @param searchDN同步節點

* @return 需要同步的帳號信息
*/
public List searchInfoByDN(String searchDN) {
List list = new ArrayList();
try {
LdapContext ctx = this.ConnLdap(InitialConf.uLDAP_URL,InitialConf.uuName, InitialConf.uPWD);
NamingEnumeration objs = ctx.search(searchDN, null);
Map<String, Object> map = null;
while (objs.hasMoreElements()) {
SearchResult match = (SearchResult) objs.nextElement();
System.out.println(match.getName());
Attributes attrs = match.getAttributes();
NamingEnumeration e = attrs.getAll();
map = new HashMap<String, Object>();
while (e.hasMoreElements()) {
Attribute attr = (Attribute) e.nextElement();
System.out.println(attr.getID());
if (attr.size() > 1) {
Object[] objclass = new String[attr.size()];
for (int i = 0; i < attr.size(); i++) {
objclass[i] = attr.get(i);
}
map.put(attr.getID(), objclass);
} else {
map.put(attr.getID(), attr.get(0));
}
}
list.add(map);
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}

/**
* 刪除指點節點數據
* @param dn
* @return
*/
public boolean deleteInformation(String dn) {
try {
LdapContext ctx = this.ConnLdap(InitialConf.uLDAP_URL,InitialConf.uuName, InitialConf.uPWD);
ctx.destroySubcontext("cn=aaa,ou=People");
return true;
} catch (NamingException ne) {
System.err.println("Error: " + ne.getMessage());
return false;
}
}

/**
* 用於修改
* @param dn
* @return
*/
public boolean modifyInformation(String dn) {
try {
LdapContext ctx = this.ConnLdap(InitialConf.uLDAP_URL,InitialConf.uuName, InitialConf.uPWD);
ModificationItem[] mods = new ModificationItem[2];
Attribute mobilePhone = new BasicAttribute("mobilePhone", "12315");
Attribute employeeName = new BasicAttribute("employeeName", "Smith");
mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,mobilePhone);
mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,employeeName);
ctx.modifyAttributes(dn, mods);
return true;
} catch (NamingException ne) {
System.err.println("Error: " + ne.getMessage());
return false;
}
}

/**
* 增加信息

* @param list 
*/

public void addToLdap(List list) {
LdapContext ctx = this.ConnLdap(InitialConf.fLDAP_URL,InitialConf.fuName, InitialConf.fPWD);
try {
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Map map = (Map) list.get(i);
Set set = map.keySet();
Iterator iterator = set.iterator();
String cn = (String) map.get("cn");

Attributes attrs = new BasicAttributes();
Attribute oneAttr = null;
while (iterator.hasNext()) {
String key = (String) iterator.next();
oneAttr = new BasicAttribute(key);
oneAttr.add(map.get(key));
attrs.put(oneAttr);
}
Attribute objClasses = new BasicAttribute("objectclass");
objClasses.add("top");
objClasses.add("simp-obj");
objClasses.add("organizationalperson");
objClasses.add("person");
objClasses.add("simp-person");
attrs.put(objClasses);
ctx.createSubcontext("cn=" + cn + "," + "ou=People", attrs);
}
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

發佈了21 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章