package encode;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.NamingEnumeration;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import java.util.Enumeration;
public class LDAPGetDomain {
public LDAPGetDomain() {
}
public void GetDomainInfo() {
Hashtable<String,String> HashEnv = new Hashtable<String,String>();
String LDAP_URL = "ldap://192.168.2.44:389"; //LDAP访问地址
String adminName = "Test.bomb//administrator"; //注意用户名的写法:domain/User 或 User@domain.com
adminName = "administrator@Test.bomb"; //注意用户名的写法:domain/User 或 User@domain.com
String adminPassword = "qqqqqqqq1!"; //密码
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
//searchFilter = "objectClass=organizationalUnit";//当然organizationalUnit表示组织结构下的组织结构
//这里解释一下 OU DC CN的意思
//因为ldap访问的是树结构的数据库 所有既然是数据库 那就必须要有 数据库名称也就是域名 这里就是用DC表示
//比如域名是Test.bomb那么必须按照顺序来写 如下 DC=Test,DC=bomb
//dc下面对应的创建文件夹就是一个组织结构 用OU来表示 OU必须DC的前面
//CN表示组织结构下的用户名称 CN必须卸载OU的前面
//主义OU必须是完整的路径 比如hongan是最上面的 下面有个a组织 a下面有个b组织
//不能直接写OU=b,OU=a,DC=Test,DC=bomb 必须要指明完整的要这样写OU=b,OU=a,OU=hongan,DC=Test,DC=bomb
//否则会报错
String searchBase = "OU=hongan,DC=Test,DC=bomb"; //Specify the Base for the search//搜索域节点
int totalResults = 0;
//String returnedAtts[] = {"memberOf"};//定制返回属性
String returnedAtts[] = {
"url", "whenChanged", "employeeID", "name", "userPrincipalName",
"physicalDeliveryOfficeName", "departmentNumber", "telephoneNumber",
"homePhone", "mobile", "department", "sAMAccountName", "whenChanged",
"mail"}; //定制返回属性
searchCtls.setReturningAttributes(returnedAtts); //设置返回属性集
//Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
System.out.println("************************************************");
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(" AttributeID=" + Attr.getID().toString());
//读取属性值
for (NamingEnumeration e = Attr.getAll(); e.hasMore();totalResults++) {
System.out.println(" AttributeValues=" + 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);
ctx.close();
}
catch (NamingException e) {
e.printStackTrace();
System.err.println("Throw Exception : " + e);
}
}
public static void main(String args[]) {
LDAPGetDomain ad = new LDAPGetDomain();
ad.GetDomainInfo();
}
}