• 通过java实现对Ldap的增删改操作


    1. 增加操作
    package com.nxw.test;
    
    import java.io.UnsupportedEncodingException;
    import java.util.Enumeration;
    import java.util.Iterator;
    
    import com.novell.ldap.LDAPAttribute;
    import com.novell.ldap.LDAPAttributeSet;
    import com.novell.ldap.LDAPConnection;
    import com.novell.ldap.LDAPEntry;
    import com.novell.ldap.LDAPException;
    import com.novell.ldap.LDAPSearchResults;
    import com.novell.ldap.util.Base64;
    
        /**
         * 
         *@author nxw
         * 
         *
         */
        public class LDAPSearchDemo {
    
            public static void main(String[] args) {
    
                String ldapHost = "localhost";
                String loginDN = "cn=Manager,dc=nxw,dc=com";
                String password = "secret";
                String searchBase = "dc=nxw,dc=com";
                String searchFilter = "objectClass=*";
    
                int ldapPort = LDAPConnection.DEFAULT_PORT;
                // 查询范围
                // SCOPE_BASE、SCOPE_ONE、SCOPE_SUB、SCOPE_SUBORDINATESUBTREE
                int searchScope = LDAPConnection.SCOPE_SUB;
    
                LDAPConnection lc = new LDAPConnection();
                try {
                    lc.connect(ldapHost, ldapPort);
                    lc.bind(LDAPConnection.LDAP_V3, loginDN, password.getBytes("UTF8"));
                    LDAPSearchResults searchResults = lc.search(searchBase,
                            searchScope, searchFilter, null, false);
    
                    while (searchResults.hasMore()) {
                        LDAPEntry nextEntry = null;
                        try {
                            nextEntry = searchResults.next();
                        } catch (LDAPException e) {
                            System.out.println("Error: " + e.toString());
                            if (e.getResultCode() == LDAPException.LDAP_TIMEOUT
                                    || e.getResultCode() == LDAPException.CONNECT_ERROR) {
                                break;
                            } else {
                                continue;
                            }
                        }
                        System.out.println("DN =: " + nextEntry.getDN());
                        System.out.println("|---- Attributes list: ");
                        LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
                        Iterator<LDAPAttribute> allAttributes = attributeSet.iterator();
                        while (allAttributes.hasNext()) {
                            LDAPAttribute attribute = allAttributes.next();
                            String attributeName = attribute.getName();
    
                            Enumeration<String> allValues = attribute.getStringValues();
                            if (null == allValues) {
                                continue;
                            }
                            while (allValues.hasMoreElements()) {
                                String value = allValues.nextElement();
                                if (!Base64.isLDIFSafe(value)) {
                                    // base64 encode and then print out
                                    value = Base64.encode(value.getBytes());
                                }
                                System.out.println("|---- ---- " + attributeName
                                        + " = " + value);
                            }
                        }
                    }
    
                } catch (LDAPException e) {
                    System.out.println("Error: " + e.toString());
                } catch (UnsupportedEncodingException e) {
                    System.out.println("Error: " + e.toString());
                } finally {
                    try {
                        if (lc.isConnected()) {
                            lc.disconnect();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    1. 删除操作
    package com.nxw.test;
    
    import java.io.UnsupportedEncodingException;
    import com.novell.ldap.LDAPConnection;
    import com.novell.ldap.LDAPException;
    
        /**
         * 删除条目的示例
         * 
         *@author nxw
         * 
         *
         */
        public class LDAPDeleteEntry {
    
            /**
             * @param args
             */
            public static void main(String[] args) {
    
                String ldapHost = "localhost";
                String loginDN = "cn=Manager,dc=voole,dc=com";
                String password = "secret";
                String deleteDN = "ou=nxw,dc=voole,dc=com";
    
                int ldapPort = LDAPConnection.DEFAULT_PORT;
                int ldapVersion = LDAPConnection.LDAP_V3;
                LDAPConnection lc = new LDAPConnection();
                try {
                    lc.connect(ldapHost, ldapPort);
                    lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
    
                    lc.delete(deleteDN);
                    System.out.println(" delete Entry: " + deleteDN + " success.");
                    lc.disconnect();
                } catch (LDAPException e) {
                    if (e.getResultCode() == LDAPException.NO_SUCH_OBJECT) {
                        System.err.println("Error: No such object");
                    } else if (e.getResultCode() == LDAPException.INSUFFICIENT_ACCESS_RIGHTS) {
                        System.err.println("Error: Insufficient rights");
                    } else {
                        System.err.println("Error: " + e.toString());
                    }
                } catch (UnsupportedEncodingException e) {
                    System.out.println("Error: " + e.toString());
                } finally {
                    try {
                        if (lc.isConnected()) {
                            lc.disconnect();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
            }
    
        }
    1. 修改操作
    package com.nxw.test;
    
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import com.novell.ldap.LDAPAttribute;
    import com.novell.ldap.LDAPConnection;
    import com.novell.ldap.LDAPException;
    import com.novell.ldap.LDAPModification;
    
        /**
         * 修改操作示例
         * 
         * @author nxw
         *
         *
         */
        public class LDAPModifyAttrs {
    
            /**
             * @param args
             */
            public static void main(String[] args) {
    
                String ldapHost = "localhost";
                String loginDN = "cn=Manager,dc=voole,dc=com";
                String password = "secret";
                String modifyDN = "ou=nxw,dc=voole,dc=com";
    
                int ldapPort = LDAPConnection.DEFAULT_PORT;
                int ldapVersion = LDAPConnection.LDAP_V3;
                LDAPConnection lc = new LDAPConnection();
    
                List<LDAPModification> modList = new ArrayList<LDAPModification>();
    
                // 向描述属性中添加一个新值
                String desc = "This object was modified at " + new Date();
                LDAPAttribute attribute = new LDAPAttribute("description", desc);
                modList.add(new LDAPModification(LDAPModification.ADD, attribute));
    
                attribute = new LDAPAttribute("telephoneNumber", "8888866666");
                modList.add(new LDAPModification(LDAPModification.ADD, attribute));
    
                // 用新值替换labeleduri地址
                attribute = new LDAPAttribute("labeledURI", "nxwtsp@163.com");
                modList.add(new LDAPModification(LDAPModification.REPLACE, attribute));
    
                // 删除email属性
                attribute = new LDAPAttribute("mail");
                modList.add(new LDAPModification(LDAPModification.DELETE, attribute));
    
                LDAPModification[] mods = new LDAPModification[modList.size()];
                mods = (LDAPModification[]) modList.toArray(mods);
    
                try {
                    lc.connect(ldapHost, ldapPort);
                    lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
                    lc.modify(modifyDN, mods);
                    System.out
                            .println("LDAPAttribute add、replace、delete all successful.");
                } catch (LDAPException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    System.out.println("Error: " + e.toString());
                } finally {
                    try {
                        if (lc.isConnected()) {
                            lc.disconnect();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
            }
    
        }

    java连接Ldap http://blog.csdn.net/nxw_tsp/article/details/52678976

    所需jar包 http://download.csdn.net/detail/nxw_tsp/9642131

    本博文引自 http://blog.csdn.net/robbin2118/article/details/42092385

  • 相关阅读:
    关键字驱动测试方法
    File文件操作
    问题处理:类没有复制构造函数
    QTP问题修改:This run was performed on Internet Explorer 7.x without the required 'Enable Tabbed Browsing
    QTP:.net中写文件
    bat垃圾清理
    Lua安装与使用
    QTP:Flight账号及密码
    如何在VS2010中运行控制台程序时停留在控制台显示窗口
    QTP中Screen Recorder无内容,no movie is associated with the results.
  • 原文地址:https://www.cnblogs.com/Silencewen/p/7471092.html
Copyright © 2020-2023  润新知