JAVA_基本LDAP操作實例
一、簡介
Lightweight Directory Access Protocol (LDAP),輕型目錄訪問協(xié)議是一個訪問在線目錄服務(wù)的協(xié)議。下面的例子中簡單介紹在java中隊ldap的增刪該查功能。目錄結(jié)構(gòu)為:
CD=CAS,DC=MYDC
--cn=users
----uid=zhangsan
二、示例
1、通過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è)置過濾條件
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);
// 三個參數(shù)分別為:
// 上下文;
// 要搜索的屬性,如果為空或 null,則返回目標(biāo)上下文中的所有對象;
// 控制搜索的搜索控件,如果為 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獲取用戶信息
這篇文章主要介紹了Spring Security如何基于Authentication獲取用戶信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03Java中StringUtils與CollectionUtils和ObjectUtil概念講解
這篇文章主要介紹了Java中StringUtils與CollectionUtils和ObjectUtil概念,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12IDEA 2020.1 搜索不到Chinese (Simplified) Language
小編在安裝中文插件時遇到IDEA 2020.1 搜索不到Chinese ​(Simplified)​ Language Pack EAP,無法安裝的問題,本文給大家分享我的解決方法,感興趣的朋友一起看看吧2020-04-04springboot業(yè)務(wù)功能實戰(zhàn)之告別輪詢websocket的集成使用
WebSocket使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot業(yè)務(wù)功能實戰(zhàn)之告別輪詢websocket的集成使用,需要的朋友可以參考下2022-10-10java數(shù)據(jù)庫連接池的特點(diǎn)及步驟
大家好,本篇文章主要講的是數(shù)據(jù)庫連接池的特點(diǎn)及步驟,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12