JAVA_基本LDAP操作實(shí)例
一、簡(jiǎn)介
Lightweight Directory Access Protocol (LDAP),輕型目錄訪問(wèn)協(xié)議是一個(gè)訪問(wèn)在線目錄服務(wù)的協(xié)議。下面的例子中簡(jiǎn)單介紹在java中隊(duì)ldap的增刪該查功能。目錄結(jié)構(gòu)為:
CD=CAS,DC=MYDC
--cn=users
----uid=zhangsan
二、示例
1、通過(guò)LdapContext連接ldap
/**
* 連接LDAP
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public LdapContext connetLDAP() throws NamingException {
// 連接Ldap需要的信息
String ldapFactory = "com.sun.jndi.ldap.LdapCtxFactory";
String ldapUrl = "ldap:/IP:port";// url
String ldapAccount = "cn=root"; // 用戶名
String ldapPwd = "password";//密碼
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, ldapFactory);
// LDAP server
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, ldapAccount);
env.put(Context.SECURITY_CREDENTIALS, ldapPwd);
env.put("java.naming.referral", "follow");
LdapContext ctxTDS = new InitialLdapContext(env, null);
return ctxTDS;
}
2、增加用戶zhangsan
// 添加
public void testAdd() throws Exception {
LdapContext ctx = connetLDAP();
Attributes attrs = new BasicAttributes(true);
Attribute objclass = new BasicAttribute("objectclass");
// 添加ObjectClass
String[] attrObjectClassPerson = { "inetOrgPerson", "organizationalPerson", "person", "top" };
Arrays.sort(attrObjectClassPerson);
for (String ocp : attrObjectClassPerson) {
objclass.add(ocp);
}
attrs.put(objclass);
String uid = "zhangsan";
String userDN = "uid=" + uid + "," + "cn=users,dc=cas,dc=mydc";
// 密碼處理
// attrs.put("uid", uid);
attrs.put("cn", uid);
attrs.put("sn", uid);
attrs.put("displayName", "張三");
attrs.put("mail", "abc@163.com");
attrs.put("description", "");
attrs.put("userPassword", "Passw0rd".getBytes("UTF-8"));
ctx.createSubcontext(userDN, attrs);
}
3、刪除用戶zhangsan
//刪除
public void testRemove() throws Exception {
LdapContext ctx = connetLDAP();
String uid = "zhangsan";
String userDN = "uid=" + uid + "," + "cn=users,dc=cas,dc=mydc";
ctx.destroySubcontext(userDN);
}
4、修改zhangsan的郵件地址
//修改
public boolean testModify() throws Exception {
boolean result = true;
LdapContext ctx = connetLDAP();
String uid = "zhangsan";
String userDN = "uid=" + uid + "," + "cn=users,dc=cas,dc=mydc";
Attributes attrs = new BasicAttributes(true);
attrs.put("mail", "zhangsan@163.com");
ctx.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attrs);
return result;
}
5、查找用戶
//查詢
public void testSearch() throws Exception {
LdapContext ctx = connetLDAP();
// 設(shè)置過(guò)濾條件
String uid = "zhangsan";
String filter = "(&(objectClass=top)(objectClass=organizationalPerson)(uid=" + uid + "))";
// 限制要查詢的字段內(nèi)容
String[] attrPersonArray = { "uid", "userPassword", "displayName", "cn", "sn", "mail", "description" };
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 設(shè)置將被返回的Attribute
searchControls.setReturningAttributes(attrPersonArray);
// 三個(gè)參數(shù)分別為:
// 上下文;
// 要搜索的屬性,如果為空或 null,則返回目標(biāo)上下文中的所有對(duì)象;
// 控制搜索的搜索控件,如果為 null,則使用默認(rèn)的搜索控件
NamingEnumeration<SearchResult> answer = ctx.search("cn=users,dc=cas,dc=mydc", filter.toString(), searchControls);
// 輸出查到的數(shù)據(jù)
while (answer.hasMore()) {
SearchResult result = answer.next();
NamingEnumeration<? extends Attribute> attrs = result.getAttributes().getAll();
while (attrs.hasMore()) {
Attribute attr = attrs.next();
System.out.println(attr.getID() + "=" + attr.get());
}
System.out.println("============");
}
}
相關(guān)文章
解決Java & Idea啟動(dòng)tomcat的中文亂碼問(wèn)題
這篇文章主要介紹了Java & Idea啟動(dòng)tomcat的中文亂碼問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Java中常用的設(shè)計(jì)模式之責(zé)任鏈模式詳解
這篇文章主要為大家詳細(xì)介紹了Java中常用的設(shè)計(jì)模式之責(zé)任鏈模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
深入分析Comparable與Comparator及Clonable三個(gè)Java接口
接口不是類,而是對(duì)類的一組需求描述,這些類要遵從接口描述的統(tǒng)一格式進(jìn)行定義,這篇文章主要為大家詳細(xì)介紹了Java的Comparable,Comparator和Cloneable的接口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-05-05
ssm框架上傳圖片保存到本地和數(shù)據(jù)庫(kù)示例
本篇文章主要介紹了ssm框架上傳圖片保存到本地和數(shù)據(jù)庫(kù)示例,主要使用了Spring+SpringMVC+MyBatis框架集合,有興趣的可以了解一下。2017-03-03
springboot整合mail實(shí)現(xiàn)郵箱的發(fā)送功能
本文分步驟給大家介紹springboot整合mail實(shí)現(xiàn)郵箱的發(fā)送功能,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09
Sparsearray稀疏數(shù)組原理及實(shí)例詳解
這篇文章主要介紹了Sparsearray稀疏數(shù)組原理及實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Java中使用Preconditions來(lái)檢查傳入?yún)?shù)介紹
這篇文章主要介紹了Java中使用Preconditions來(lái)檢查傳入?yún)?shù)介紹,本文只是作為一個(gè)簡(jiǎn)單的用法介紹,需要的朋友可以參考下2015-06-06
淺談SpringBoot項(xiàng)目打成war和jar的區(qū)別
這篇文章主要介紹了淺談SpringBoot項(xiàng)目打成war和jar的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

