• java连接Ldap


    java连接Ldap 仅做个人学习之用

    package com.nxw.test;
    
    import java.util.Enumeration;
    import java.util.Hashtable;   
    import java.util.UUID;
    
    import javax.naming.Context;  
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException; 
    import javax.naming.directory.Attribute;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.DirContext;  
    import javax.naming.directory.InitialDirContext;   
    import javax.naming.directory.SearchControls;
    import javax.naming.directory.SearchResult;
    import javax.naming.ldap.InitialLdapContext;
    import javax.naming.ldap.LdapContext;
    public class Ldap {   
    
        public static void main(String[] args) {
            String url = "ldap://127.0.0.1:389/";
            String domain = "dc=nxw,dc=org";
            String user = "cn=manager";
            String password = "secret";
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP 工厂
            env.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
            env.put(Context.PROVIDER_URL, url);
            env.put(Context.SECURITY_PRINCIPAL, user+","+domain); //  填DN
            env.put(Context.SECURITY_CREDENTIALS, password); // AD Password
            env.put("java.naming.ldap.attributes.binary", "objectSid objectGUID");
            LdapContext ldapCtx = null;
            try {
                ldapCtx = new InitialLdapContext(env , null);
                queryGroup(ldapCtx);    
    
            } catch (NamingException e) {
                e.printStackTrace();
            } finally {
                if(ldapCtx != null) {
                    try {
                        ldapCtx.close();
                    } catch (NamingException e) {
                    }
                }
            }
        }
    
        private static void queryGroup(LdapContext ldapCtx) throws NamingException {
            SearchControls searchCtls = new SearchControls();
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            String searchFilter = "objectClass=organizationalUnit";
            String searchBase = "dc=nxw,dc=org";
            String returnedAtts[] = {"distinguishedName", "objectGUID", "name"};
            searchCtls.setReturningAttributes(returnedAtts);
            NamingEnumeration<SearchResult> answer = ldapCtx.search(searchBase, searchFilter, searchCtls);
            while (answer.hasMoreElements()) {
                SearchResult sr = answer.next();
                Attributes Attrs = sr.getAttributes();
                if (Attrs != null) {
                    NamingEnumeration<?> ne = Attrs.getAll();
                    while(ne.hasMore()) {
                        Attribute Attr = (Attribute)ne.next();
                        String name = Attr.getID();
                        Enumeration<?> values = Attr.getAll();
                        if (values != null) { // 迭代
                            while (values.hasMoreElements()) {
                                String value = "";
                                if("objectGUID".equals(name)) {
                                    value = UUID.nameUUIDFromBytes((byte[]) values.nextElement()).toString();
                                } else {
                                    value = (String)values.nextElement();
                                }
                                System.out.println(name + " " + value);
                            }
                        }
                    }
                    System.out.println("=====================success!");
                }
            }
    
        }
    }

    通过java实现对Ldap的增删改操作 http://blog.csdn.net/nxw_tsp/article/details/52690376

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

    本博文参考 http://blog.csdn.net/zfpigpig/article/details/21176697

  • 相关阅读:
    Stm32CubeMX5 配置 STM32的串口DMA接受方式 --- 基于 stm32f051k8u6
    Stm32 控制1.44寸液晶显示图片 基于stm32f051k8u6
    makefile自动编译
    Stm32CubeMX5 创建LED控制工程
    ARM 汇编与C之间 的调用
    shell 脚本文件类型.sh ,变量
    bzoj3589 动态树 求链并 容斥
    bzoj2287【POJ Challenge】消失之物 缺一01背包
    bzoj2916: [Poi1997]Monochromatic Triangles 思路
    [NOI2010]超级钢琴 主席树
  • 原文地址:https://www.cnblogs.com/Silencewen/p/7471094.html
Copyright © 2020-2023  润新知