更多解释信息请百度,哈哈哈哈哈哈哈
public class AdUtil
{
private static Logger logger = Logger.getLogger(AdUtil.class);
public static String uimurl = PropertiesUtil.readValue("gzhr_info", "zgc.uim.rest");
public static String uimrestproxy = PropertiesUtil.readValue("gzhr_info", "uim.proxy.username");
public static String proxypassword = PropertiesUtil.readValue("gzhr_info", "uim.proxy.password");
public static String uim_ou_addn = PropertiesUtil.readValue("gzhr_info", "uim.ou.addn");
public static String uim_ou_oucode = PropertiesUtil.readValue("gzhr_info", "uim.ou.oucode");
public static String uim_ou_gzouid = PropertiesUtil.readValue("gzhr_info", "uim.ou.gzouid");
LdapContext context = null;
private String ldapHost;
private int ldapVersion;
private int ldapport;
private int ldapScope;
private String ldapUser;
private String password;
private String baseDn;
/**
* ldapHost:IP地址
* ldapVersion:ldap版本
* ldapport:端口
* ldapScope:查询域
* ldapUser:管理员
* password:密码
* baseDn:根节点
* @param adName
*/
public void initProperties(String adName) {
this.ldapHost = PropertiesUtil.readValue(adName, "ldapHost");
this.ldapVersion = Integer.parseInt(PropertiesUtil.readValue(adName, "ldapVersion"));
this.ldapport = Integer.parseInt(PropertiesUtil.readValue(adName, "ldapport"));
this.ldapScope = Integer.parseInt(PropertiesUtil.readValue(adName, "ldapScope"));
this.ldapUser = PropertiesUtil.readValue(adName, "ldapUser");
this.password = PropertiesUtil.readValue(adName, "password");
this.baseDn = PropertiesUtil.readValue(adName, "baseDn");
}
/**
* 初始化链接
* @return
* @throws Exception
*/
public LdapContext getcon()
throws Exception
{
String AUTH_TYPE = "simple";
String CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
Hashtable<String , String> env = new Hashtable<String , String>();
env.put("java.naming.factory.initial", CONTEXT_FACTORY);
env.put("java.naming.security.authentication", AUTH_TYPE);
env.put("java.naming.provider.url", "ldap://" + this.ldapHost + ":" + this.ldapport + "/" + this.baseDn);
env.put("java.naming.security.principal", this.ldapUser);
env.put("java.naming.security.credentials", this.password);
return new InitialLdapContext(env,
null);
}
/**
* 关闭链接
*/
public void closead()
{
if (this.context != null)
try {
this.context.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
public List<Map<String,String>> listAllUser(String type)
{
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
// 域节点 OU=办公室,OU=测试集团,DC=cstest,DC=com,查询该节点以下的所有符合条件的数据
String searchBase = "OU=安全保卫监督部,OU=测试集团";
// LDAP搜索过滤器类
// cn=*name*模糊查询 cn=name 精确查询 ,String searchFilter ="(&(objectClass="+type+")("+filter+"=*"+name+"*))";
String searchFilter ="(objectClass="+type+")";
try
{
this.context = getcon();
// 创建搜索控制器
SearchControls searchCtls = new SearchControls();
// 设置搜索范围
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 设置返回属性集 不设置则返回所有属性
String[] returnedAtts = {"samaccountname","cn","distinguishedname","mobile","mail","department","objectclass"};
searchCtls.setReturningAttributes(returnedAtts);
// 根据设置的域节点、过滤器类和搜索控制器搜索LDAP得到结果
NamingEnumeration entries = this.context.search(searchBase, searchFilter, searchCtls);
// 初始化搜索结果数为0
int totalResults = 0;
int rows = 0;
while(entries.hasMoreElements()){
// 得到符合搜索条件的DN
SearchResult entry = (SearchResult)entries.next();
++rows;
String userName = entry.getName();
System.out.println("用户:"+userName);
// 得到符合条件的属性集
Attributes at = entry.getAttributes();
if (at != null) {
Map map = new HashMap();
for(NamingEnumeration ne = at.getAll(); ne.hasMore(); ) {
// 得到下一个属性
Attribute Attr = (Attribute)ne.next();
//属性名
String attrid=Attr.getID().toString();
System.out.print(attrid+'\t');
// 读取属性值
for(NamingEnumeration e = Attr.getAll(); e.hasMore(); totalResults++){
//属性值 objectClass有四个值top、 person、 organizationalPerson、 user,但put的时候key一样,所以value会覆盖前面的值
String attrvalue =e.next().toString();
System.out.print(attrvalue+" ");
map.put(attrid,attrvalue);
}
System.out.println();
}
list.add(map);
}
System.out.println("---------------");
}
System.out.println("************************************************");
System.out.println("Number: " + totalResults);
System.out.println("总共用户数:"+rows);
}catch (NamingException e){
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
closead();
}
return list;
}
//---------------------MAIN----------------
public static void main(String[] args) {
AdUtil adUtil = new AdUtil();
adUtil.initProperties("csad");
List<Map<String,String>> list= adUtil.listAllUser("organizationalPerson");
for(int i=0,lengths=list.size();i<lengths;i++){
Map<String,String> map = list.get(i);
for(Map.Entry<String,String> keyset:map.entrySet()){
System.out.println("Key:"+keyset.getKey()+",Value:"+keyset.getValue());
}
System.out.println("-----------------------------");
}
}
}
输出为:
用户:CN=唐涛
mail tang.tao@tt.com
mobile 13999999991
department 安全监督部
objectClass top person organizationalPerson user
sAMAccountName tang.songtao
distinguishedName CN=唐涛,OU=安全保卫监督部,OU=测试集团,DC=cstest,DC=com
cn 唐涛
---------------
用户:CN=曾为昀
mail zeng.weiyun@tt.com
mobile 18664888888
department 安全监督部
objectClass top person organizationalPerson user
sAMAccountName zeng.yun
distinguishedName CN=曾为昀,OU=安全保卫监督部,OU=测试集团,DC=cstest,DC=com
cn 曾为昀
************************************************
Number: 38
总共用户数:4
Key:mail,Value:tang.tao@tt.com
Key:cn,Value:唐涛
Key:sAMAccountName,Value:tang.tao
Key:department,Value:安全监督部
Key:objectClass,Value:user
Key:distinguishedName,Value:CN=唐涛,OU=安全保卫监督部,OU=测试集团,DC=cstest,DC=com
Key:mobile,Value:139999999991
-----------------------------
Key:mail,Value:zeng.yun@tt.com
Key:cn,Value:曾为昀
Key:sAMAccountName,Value:zeng.weiyun
Key:department,Value:安全监督部
Key:objectClass,Value:user
Key:distinguishedName,Value:CN=曾为昀,OU=安全保卫监督部,OU=测试集团,DC=cstest,DC=com
Key:mobile,Value:18664888888