通过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);
}