Last active
October 6, 2019 10:37
-
-
Save imrexhuang/dfbad1e0a0d3128e33694bcf91b0dfb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//來源: https://www.cnblogs.com/qlong8807/archive/2012/10/26/2741563.html | |
package ldap.a; | |
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 LdapReadInfo { | |
public static void main(String args[]) { | |
Hashtable HashEnv = new Hashtable(); | |
String LDAP_URL = "ldap://192.168.10.56:389"; // LDAP訪問位址,389是傳輸不加密;加密的LDAPS port是 636 | |
String adminName = "domainName\\administrator"; //注意用戶名的寫法:domain\User 或// User@domain.com | |
String adminPassword = mypassword"; // 密碼 | |
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, LDAP_URL); | |
try { | |
LdapContext ctx = new InitialLdapContext(HashEnv, null); | |
SearchControls searchCtls = new SearchControls(); // Create the | |
// search | |
// controls | |
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify | |
// the | |
// search | |
// scope | |
String searchFilter = "objectClass=user"; // specify the LDAP search | |
// filter ,這裡也可以設置為:objectClass=computer"; | |
String searchBase = "DC=jasgroup,DC=com"; // Specify the Base for | |
// the search//搜索域節點 | |
int totalResults = 0; | |
int totalAnswer = 0; | |
String returnedAtts[] = { "url", "whenChanged", "employeeID", | |
"name", "userPrincipalName", "physicalDeliveryOfficeName", | |
"departmentNumber", "telephoneNumber", "homePhone", | |
"mobile", "department", "sAMAccountName", "mail" }; // 定制返回屬性,有些不一定您公司的Active Directory有這些屬性,請自行調整 | |
searchCtls.setReturningAttributes(returnedAtts); // 設置返回屬性集,這裡如果設為null,那麼就是返回所有的屬性。 | |
// Search for objects using the filter | |
NamingEnumeration answer = ctx.search(searchBase, searchFilter, | |
searchCtls); | |
if (answer == null || answer.equals(null)) { | |
System.out.println("answer is null"); | |
} else { | |
System.out.println("answer not null"); | |
} | |
while (answer.hasMoreElements()) { | |
SearchResult sr = (SearchResult) answer.next(); | |
System.out.println("************************************************"); | |
totalAnswer++; | |
System.out.println(sr.getName()); | |
Attributes Attrs = sr.getAttributes(); | |
if (Attrs != null) { | |
try { | |
for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) { | |
Attribute Attr = (Attribute) ne.next(); | |
System.out.println(" AttrName="+ Attr.getID().toString()); | |
int i =0; | |
// 讀取屬性值 | |
for (NamingEnumeration e = Attr.getAll(); e.hasMore(); totalResults++,i++) { | |
System.out.println(" AttrValue["+i+"]="+ e.next().toString()); | |
} | |
System.out.println(" ---------------"); | |
// 讀取屬性值 | |
// Enumeration values = Attr.getAll(); | |
// if (values != null) { // 反覆運算 | |
// while (values.hasMoreElements()) { | |
// System.out.println(" AttributeValues="+ values.nextElement()); | |
// } | |
// } | |
// System.out.println(" ---------------"); | |
} | |
} catch (NamingException e) { | |
System.err.println("Throw Exception : " + e); | |
} | |
} | |
} | |
System.out.println("Number: " + totalResults); | |
System.out.println("totalAnswer:"+totalAnswer); | |
ctx.close(); | |
} | |
catch (NamingException e) { | |
e.printStackTrace(); | |
System.err.println("Throw Exception : " + e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment