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"; // 用戶(hù)名
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、增加用戶(hù)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、刪除用戶(hù)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、查找用戶(hù)
//查詢(xún)
public void testSearch() throws Exception {
LdapContext ctx = connetLDAP();
// 設(shè)置過(guò)濾條件
String uid = "zhangsan";
String filter = "(&(objectClass=top)(objectClass=organizationalPerson)(uid=" + uid + "))";
// 限制要查詢(xún)的字段內(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)文章
Spring Security如何基于Authentication獲取用戶(hù)信息
這篇文章主要介紹了Spring Security如何基于Authentication獲取用戶(hù)信息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Java中StringUtils與CollectionUtils和ObjectUtil概念講解
這篇文章主要介紹了Java中StringUtils與CollectionUtils和ObjectUtil概念,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12IDEA 2020.1 搜索不到Chinese (Simplified) Language
小編在安裝中文插件時(shí)遇到IDEA 2020.1 搜索不到Chinese ​(Simplified)​ Language Pack EAP,無(wú)法安裝的問(wèn)題,本文給大家分享我的解決方法,感興趣的朋友一起看看吧2020-04-04springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢(xún)websocket的集成使用
WebSocket使得客戶(hù)端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡(jiǎn)單,允許服務(wù)端主動(dòng)向客戶(hù)端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢(xún)websocket的集成使用,需要的朋友可以參考下2022-10-10java數(shù)據(jù)庫(kù)連接池的特點(diǎn)及步驟
大家好,本篇文章主要講的是數(shù)據(jù)庫(kù)連接池的特點(diǎn)及步驟,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽2021-12-12java實(shí)現(xiàn)在一張大圖片上添加小圖及文字
這篇文章主要介紹了java實(shí)現(xiàn)在一張大圖上添加小圖及文字,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11volatile可見(jiàn)性的一些認(rèn)識(shí)和論證
volatile的關(guān)鍵詞的使用在JVM內(nèi)存模型中已是老生常談了,這篇文章主要結(jié)合自己對(duì)可見(jiàn)性的一些認(rèn)識(shí)和一些直觀的例子來(lái)談?wù)剉olatile,感興趣的朋友一起看看吧2017-08-08