java Ldap域信息DN信息获取
- 区别于AD域认证,添加基准的区别
// 其中value值为cn=manager,cn=lff,cn=com
// String domain = "cn=lff,cn=com"
env.put(Context.SECURITY_PRINCIPAL, "cn=" + uName + "," + domain);
// AD域
// env.put(Context.SECURITY_PRINCIPAL, uName);
2.数据获取
public static List<LdapModel> returnLdapResult(Properties env, String sDomain) {
List<LdapModel> ldapModels = new ArrayList<LdapModel>();
try {
DirContext ctx = new InitialLdapContext(env, null);
String basedn = sDomain;
String user_name = "";
// 过滤条件
String filter = "(&(objectClass=*)(uid=*))";
String[] attrPersonArray = {"uid", "o"};
SearchControls searchControls = new SearchControls();// 搜索控件
searchControls.setSearchScope(2);// 搜索范围
searchControls.setReturningAttributes(attrPersonArray);
// 1.要搜索的上下文或对象的名称;2.过滤条件,可为null,默认搜索所有信息;3.搜索控件,可为null,使用默认的搜索控件
NamingEnumeration<SearchResult> answer = ctx.search(basedn, filter.toString(), searchControls);
while (answer.hasMoreElements()) {// 遍历结果集
SearchResult result2 = (SearchResult) answer.next();
String dnString = result2.getNameInNamespace();
// dnString格式为:"uid=dongyi,ou=people,dc=lff,dc=com"
if (dnString.startsWith("uid")) {
LdapModel ldapModel = new LdapModel();
ldapModel.setAttrValue(dnString.split(",")[0].split("=")[1]);
ldapModel.setAttrName(dnString.split(",")[0].split("=")[1]);
ldapModel.setDn(dnString);
ldapModels.add(ldapModel);
}
}
return ldapModels;
} catch (NamingException e) {
e.printStackTrace();
}
return ldapModels;
}
3.DN