欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA_基本LDAP操作實例

 更新時間:2013年09月17日 16:24:53   作者:  
這篇文章介紹了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

復(fù)制代碼 代碼如下:

/**
 * 連接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
復(fù)制代碼 代碼如下:
 
// 添加 
    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

復(fù)制代碼 代碼如下:

//刪除 
    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的郵件地址

復(fù)制代碼 代碼如下:
 
//修改 
    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、查找用戶
復(fù)制代碼 代碼如下:

//查詢 
    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事件監(jiān)聽詳解

    Spring事件監(jiān)聽詳解

    這篇文章主要介紹了Spring事件監(jiān)聽詳解,文中有非常詳細(xì)的圖文解說及代碼示例,對正在學(xué)習(xí)java Spring的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • Spring Security如何基于Authentication獲取用戶信息

    Spring Security如何基于Authentication獲取用戶信息

    這篇文章主要介紹了Spring Security如何基于Authentication獲取用戶信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Java中StringUtils與CollectionUtils和ObjectUtil概念講解

    Java中StringUtils與CollectionUtils和ObjectUtil概念講解

    這篇文章主要介紹了Java中StringUtils與CollectionUtils和ObjectUtil概念,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-12-12
  • IDEA 2020.1 搜索不到Chinese ​(Simplified)​ Language Pack EAP,無法安裝的問題

    IDEA 2020.1 搜索不到Chinese ​(Simplified)​ Language

    小編在安裝中文插件時遇到IDEA 2020.1 搜索不到Chinese &#8203;(Simplified)&#8203; Language Pack EAP,無法安裝的問題,本文給大家分享我的解決方法,感興趣的朋友一起看看吧
    2020-04-04
  • springboot業(yè)務(wù)功能實戰(zhàn)之告別輪詢websocket的集成使用

    springboot業(yè)務(wù)功能實戰(zhàn)之告別輪詢websocket的集成使用

    WebSocket使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot業(yè)務(wù)功能實戰(zhàn)之告別輪詢websocket的集成使用,需要的朋友可以參考下
    2022-10-10
  • java數(shù)據(jù)庫連接池的特點(diǎn)及步驟

    java數(shù)據(jù)庫連接池的特點(diǎn)及步驟

    大家好,本篇文章主要講的是數(shù)據(jù)庫連接池的特點(diǎn)及步驟,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • JavaPoet的使用指南小結(jié)

    JavaPoet的使用指南小結(jié)

    這篇文章主要介紹了JavaPoet的使用指南小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • java實現(xiàn)在一張大圖片上添加小圖及文字

    java實現(xiàn)在一張大圖片上添加小圖及文字

    這篇文章主要介紹了java實現(xiàn)在一張大圖上添加小圖及文字,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • volatile可見性的一些認(rèn)識和論證

    volatile可見性的一些認(rèn)識和論證

    volatile的關(guān)鍵詞的使用在JVM內(nèi)存模型中已是老生常談了,這篇文章主要結(jié)合自己對可見性的一些認(rèn)識和一些直觀的例子來談?wù)剉olatile,感興趣的朋友一起看看吧
    2017-08-08
  • Spring Boot 整合 Druid過程解析

    Spring Boot 整合 Druid過程解析

    這篇文章主要介紹了Spring Boot 整合 Druid過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11

最新評論