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

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值