java獲取windows Active Directory信息


     通過java原生態的api就可以獲取到windows AD信息,想要了解windows Active Directory是個什么東東,自己可以查閱相關資料,一般來說windows系統安裝完后可能不會自帶AD查看器,我們需要通過下載軟件進行update,下載地址:https://www.technipages.com/windows-install-active-directory-users-and-computers,安裝完AD我們可以通過AD查看器查看到如下信息,我們還可以通過一些工具比如LDAPSoft Ldap Browse查看AD信息:


上圖就是我的windows電腦所顯示的AD信息,我的是win10系統,上面顯示的屬性中我們可以通過java api獲取到

得到LdapContext信息,要獲取到LdapContext信息,我們需要提供開通389端口的ldap協議的電腦ip、domain、用戶名及密碼信息,否則獲取會報錯

public class LdapContextFactory {

private static LdapContext ldapContext;

public static LdapContext getLdapContext(String url, String domain, String username, String password) {
if (ldapContext == null) {
try {
Hashtable<String, String> environment = getActiveDirectoryEnvironment(url, domain, username, password);
ldapContext = new InitialLdapContext(environment, null);
} catch (NamingException e) {
e.printStackTrace();
}
}
return ldapContext;
}

private static Hashtable<String, String> getActiveDirectoryEnvironment(String url, String domain, String username,
String password) {
Hashtable<String, String> environment = new Hashtable<>();
environment.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
environment.put("java.naming.security.authentication", "simple");
environment.put("java.naming.provider.url", url);
environment.put("java.naming.security.principal", username + "@" + domain);
environment.put("java.naming.security.credentials", password);
return environment;
}

}

獲取所有屬性信息

	public static void getAllAttribute(LdapContext ldapContext){
try {
System.out.println("validate success :" + ldapContext);
Name name = new LdapName("dc=centmfademo,dc=com");
Attributes allAttrs = ldapContext.getAttributes(name);
if (null == allAttrs) {
System.out.println("no attributes");
return;
}
for (NamingEnumeration<?> attrs = allAttrs.getAll(); attrs.hasMore();) {
Attribute attr = (Attribute) attrs.next();
System.out.println("attr : " + attr.getID());
for (NamingEnumeration<?> values = attr.getAll(); values.hasMore();) {
System.out.println("\tvalue : " + values.next());
}
}

System.out.println("-------------------------------------------");
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
System.out.println("validate faliure :" + e);
} finally {
try {
ldapContext.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
獲取定制屬性信息:

	public static void getSpecialAttribute(LdapContext ldapContext){
try{
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String returnedAtts[] = {"memberOf","dSCorePropagationData"};//定制返回屬性
/*String returnedAtts[] = { "url", "whenChanged", "employeeID", "name", "userPrincipalName",
"physicalDeliveryOfficeName", "departmentNumber", "telephoneNumber", "homePhone", "mobile",
"department", "sAMAccountName", "whenChanged", "mail" }; // 定制返回屬性
*/ searchControls.setReturningAttributes(returnedAtts); // 設置返回屬性集

String searchBase = "DC=centmfademo,DC=com"; //Specify the Base for the search//搜索域節點

//(&(objectClass=user))
//(&(objectClass=user)(sAMAccountName=dev001))
//(&(objectClass=user)(sAMAccountName=dev001)(givenName=dev))
String searchFilter = "objectClass=User"; //specify the LDAP search filter
//String searchFilter = "objectClass=organizationalUnit";//specify the LDAP search filter
NamingEnumeration<SearchResult> searchResults = ldapContext.search(searchBase, searchFilter,searchControls);
while(searchResults.hasMoreElements()){
SearchResult searchResult = searchResults.next();
System.out.println(searchResult.getName());
Attributes allAttrs = searchResult.getAttributes();
//System.out.println(allAttrs.get("mail"));
if (null == allAttrs) {
System.out.println("no attributes");
return;
}
for (NamingEnumeration<?> attrs = allAttrs.getAll(); attrs.hasMore();) {
Attribute attr = (Attribute) attrs.next();
System.out.println("attr : " + attr.getID());
//System.out.println("attr get : " + attr.get().toString());
for (NamingEnumeration<?> values = attr.getAll(); values.hasMore();) {
System.out.println("\tvalue : " + values.next());
}

/*Enumeration<?> values = attr.getAll();
if(values != null){
while(values.hasMoreElements()){
System.out.println(" AttributeValues=" + values.nextElement());
}
}*/
}
}
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
System.out.println("validate faliure :" + e);
} finally {
try {
ldapContext.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
編寫我們的測試類進行測試

	public static void main(String[] args) {
String url = "ldap://127.0.0.1:389";// 19.201.
String domain = "testademo.com";
String username = "administrator"; // 用戶名稱
String password = "testhello"; // 密碼

LdapContext ldapContext = LdapContextFactory.getLdapContext(url, domain, username, password);
getAllAttribute(ldapContext);

getSpecialAttribute(ldapContext);
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com