java ldap讀取方法

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentLinkedQueue;

import static org.junit.Assert.assertEquals;


//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = Application.class)
public class ControllerTest {


    @Test
    public void test(){
        //System.out.println(commonUtil.getAssetCode());

        Hashtable env = new Hashtable();
        String LDAP_URL = "ldap://ip:389"; // LDAP 訪問地址
        String adminName = ""; // 注意用戶名的寫法:domain\User 或 cn=user
        String adminPassword = ""; // 密碼
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, LDAP_URL);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, adminName);
        env.put(Context.SECURITY_CREDENTIALS, adminPassword);
        InitialDirContext dc = null;
        try {
            dc = new InitialDirContext(env);// 初始化上下文
            System.out.println("認證成功");


        } catch (javax.naming.AuthenticationException e) {
            System.out.println("認證失敗");
        } catch (Exception e) {
            System.out.println("認證出錯:" + e);
        }

        // 建立搜索控件
        SearchControls searchCtls = new SearchControls();
        // 設置搜索範圍
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        // 設置搜索過濾條件
        String searchFilter = "sAMAccountName=" + "";
        // 設置搜索域節點
        String searchBase = "ou=傳化化學集團,ou=全體人員,dc=etransfar,dc=com";
        // 定製返回屬性
        String returnedAtts[] = { "ou", "displayname","cn","dn"};//displayname 姓名 ou 組織 cn 工號
        searchCtls.setReturningAttributes(returnedAtts);
        // 不定製屬性,返回全部的屬性集
        //searchCtls.setReturningAttributes(null);
        int totalResults = 0;
        try {
            NamingEnumeration answer = dc.search(searchBase, "(objectClass=person)", searchCtls);
            while (answer.hasMoreElements()) {
                SearchResult sr = (SearchResult) answer.next();
                Attributes Attrs = sr.getAttributes();
                System.out.println(sr.getName());
                if (Attrs != null) {
                    try {
                        for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) {
                            Attribute Attr = (Attribute) ne.next();
                            // 讀取屬性值
                            for (NamingEnumeration e = Attr.getAll(); e.hasMore();) {
                                // 接受循環遍歷讀取的userPrincipalName用戶屬性
                                String user = e.next().toString();
                                System.out.println(" user=" + user);

                            }
/*                            // 讀取屬性值
                             Enumeration values = Attr.getAll();
                             if (values != null) {
                                  while (values.hasMoreElements()) {
                                          System.out.println(" AttributeValues=" + values.nextElement());
                                  }
                            }*/


                        }
                    } catch (NamingException e) {
                        System.err.println("Throw Exception : " + e);
                    }
                }
                totalResults++;
                System.out.println("--------------------------");
            }
            System.out.println(totalResults);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Throw Exception : " + e);
        }
    }


}
相關文章
相關標籤/搜索