程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA_根本LDAP操作實例

JAVA_根本LDAP操作實例

編輯:關於JAVA

JAVA_根本LDAP操作實例。本站提示廣大學習愛好者:(JAVA_根本LDAP操作實例)文章只能為提供參考,不一定能成為您想要的結果。以下是JAVA_根本LDAP操作實例正文


1、簡介

Lightweight Directory Access Protocol (LDAP),輕型目次拜訪協定是一個拜訪在線目次辦事的協定。上面的例子中簡略引見在java中隊ldap的增刪該查功效。目次構造為:

CD=CAS,DC=MYDC

--cn=users

----uid=zhangsan

2、示例
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", "[email protected]"); 
        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", "[email protected]"); 
        ctx.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attrs); 
        return result; 

    } 

5、查找用戶

//查詢 
    public void testSearch() throws Exception { 
        LdapContext ctx = connetLDAP(); 
        // 設置過濾前提 
        String uid = "zhangsan"; 
        String filter = "(&(objectClass=top)(objectClass=organizationalPerson)(uid=" + uid + "))"; 
        // 限制要查詢的字段內容 
        String[] attrPersonArray = { "uid", "userPassword", "displayName", "cn", "sn", "mail", "description" }; 
        SearchControls searchControls = new SearchControls(); 
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
        // 設置將被前往的Attribute 
        searchControls.setReturningAttributes(attrPersonArray); 
        // 三個參數分離為: 
        // 高低文; 
        // 要搜刮的屬性,假如為空或 null,則前往目的高低文中的一切對象; 
        // 掌握搜刮的搜刮控件,假如為 null,則應用默許的搜刮控件 
        NamingEnumeration<SearchResult> answer = ctx.search("cn=users,dc=cas,dc=mydc", filter.toString(), searchControls); 
        // 輸入查到的數據 
        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("============"); 
        } 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved