標籤:
package demo;import java.util.Hashtable;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.SearchControls;import javax.naming.directory.SearchResult;import javax.naming.ldap.InitialLdapContext;import javax.naming.ldap.LdapContext;public class ADOperTest { public void GetADInfo(boolean isUser) { String host = "192.168.1.188"; // AD伺服器 String port = "389"; // 連接埠 String url = new String("ldap://" + host + ":" + port); Hashtable HashEnv = new Hashtable(); String adminName = "[email protected]"; // 注意使用者名稱的寫法:domain\User String adminPassword = "2015"; // 密碼 HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP訪問安全層級 HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); // AD Password HashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工廠類 HashEnv.put(Context.PROVIDER_URL, url); try { LdapContext ctx = new InitialLdapContext(HashEnv, null); // 域節點 String searchBase = "OU=廣州日報集團,DC=gzrb,DC=local"; // LDAP搜尋過濾器類 String searchFilter = isUser ? "(&(objectClass=user))" : "(&(objectClass=organizationalUnit))"; // 搜尋控制器 SearchControls searchCtls = new SearchControls(); // Create the // 建立搜尋控制器 searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify // 定製返回屬性 String[] returnedAtts = null; if (isUser) { returnedAtts = new String[] { "sAMAccountName", "distinguishedName", "name" }; } else { returnedAtts = new String[] { "ou", "distinguishedName", "name" }; } searchCtls.setReturningAttributes(returnedAtts); // 設定返回屬性集 // 根據設定的域節點、過濾器類和搜尋控制器搜尋LDAP得到結果 NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);// Search for objects using the filter while (answer.hasMoreElements()) {// 遍曆結果集 SearchResult sr = (SearchResult) answer.next();// 得到符合搜尋條件的DN String dn = sr.getAttributes().get("distinguishedName").get() .toString(); System.out.println(dn); Attributes Attrs = sr.getAttributes();// 得到合格屬性集 if (Attrs != null) { try { for (NamingEnumeration ne = Attrs.getAll(); ne .hasMore();) { Attribute Attr = (Attribute) ne.next();// 得到下一個屬性 System.out.print(" 屬性名稱:" + Attr.getID().toString()); // 讀取屬性值 for (NamingEnumeration e = Attr.getAll(); e .hasMore();) { String val = e.next().toString(); System.out.println(" 屬性值:" + val); } } } catch (NamingException e) { System.err.println("Throw Exception : " + e); } }// if } ctx.close(); } catch (NamingException e) { e.printStackTrace(); System.err.println("Throw Exception : " + e); } } public void login() { String userName = "[email protected]"; // 使用者名稱稱 String password = "2015"; // 密碼 String host = "192.168.1.188"; // AD伺服器 String port = "389"; // 連接埠 String domain = "@hotent.local"; // 郵箱的尾碼名 String url = new String("ldap://" + host + ":" + port); String user = userName.indexOf(domain) > 0 ? userName : userName + domain; Hashtable env = new Hashtable(); LdapContext ctx = null; env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, user); // 不帶郵箱尾碼名的話,會報錯,具體原因還未探究。高手可以解釋分享。 env.put(Context.SECURITY_CREDENTIALS, password); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, url); try { ctx = new InitialLdapContext(env, null); ctx.close(); System.out.println("驗證成功!"); } catch (NamingException err) { err.printStackTrace(); System.out.println("驗證失敗!"); } } public static void main(String args[]) { // 執行個體化 ADOperTest ad = new ADOperTest(); ad.GetADInfo(true);// System.out.println("---------組織---------");// ad.GetADInfo(false); ad.login(); }}這樣遍曆系統中的使用者,組織,和登入。
java 訪問活動目錄代碼