public static List<TSUser> getUserList() { List<TSUser> users = new ArrayList<TSUser>(); // 域节点 SearchControls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // 对象的每个属性名 String[] returnedAtts = { "sAMAccountName","displayName","Description" }; searchCtls.setReturningAttributes(returnedAtts); NamingEnumeration<SearchResult> answer; try { getLdapContext(); char c; for(c = 'A'; c <= 'Z'; ++c){ // 过滤器,可改变查询条件 String searchFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName="+c+"*))"; answer = ctx.search(BASEDN , searchFilter, searchCtls); while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); TSUser user = new TSUser(); NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll(); while (attrs.hasMore()) { Attribute attr = attrs.next(); // 账户 if(attr.getID().equals("sAMAccountName")) { user.setUserName(attr.get().toString()); } // 姓名 if(attr.getID().equals("displayName")) { user.setRealName(attr.get().toString()); } // 工号 if(attr.getID().equals("Description")) { user.setBadge(attr.get().toString()); } } users.add(user); } } } catch (NamingException e) { e.printStackTrace(); } finally { close(); } return users; }
Java读取Ldap中所有用户
最新推荐文章于 2024-04-01 15:34:30 发布