gpt4 book ai didi

返回不完整用户列表的 Java Active Directory 查询

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:01 26 4
gpt4 key购买 nike

我想用 Java 列出所有 AD 用户。我正在使用这段代码:

String ldapUri = "ldap://" + serverName;
LdapContext ctx = null;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "Simple");
//it can be <domain\\userid> something that you use for windows login
//it can also be
env.put(Context.SECURITY_PRINCIPAL, adminName);
try {
env.put(Context.SECURITY_CREDENTIALS, adminPass.getBytes("UTF8"));
env.put(Context.REFERRAL, "follow");
} catch (java.io.UnsupportedEncodingException e) {
log.error("Non-Fatal exception : ", e);
/* ignore */
}
//in following property we specify ldap protocol and connection url.
//generally the port is 389
env.put(Context.PROVIDER_URL, ldapUri);

log.info("AD Server: " + ldapUri + ", admin " + adminName);

ctx = new InitialLdapContext(env, null);

DirContext ctx1 = new InitialDirContext(env);
SearchControls ctls = new SearchControls();
String[] attrIDs = {"distinguishedName", "cn", "name", "uid",
"sn",
"name",
"memberOf",
"displayName",
"userPrincipalName"};

ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration answer = ctx1.search(searchPath, "(&(objectClass=user)(objectCategory=person))", ctls);
while (answer.hasMoreElements()) {
// Process user
SearchResult rslt = (SearchResult) answer.next();
}

该代码在大多数环境中都能正常工作,但有一位客户报告说缺少一些用户。我已尝试对其进行故障排除,但未列出用户,但使用 Active Directory 管理员或 Active Directory Explorer 列出了他们。

有什么想法吗?

最佳答案

我假设您使用的帐户具有足够的权限。据我所知,默认情况下域 Controller 的任何实例都会返回 1000 个对象。您很可能遇到这种情况。您必须使用 LDAP 分页才能解决此问题。查看 JNDI 页面控件 - https://docs.oracle.com/javase/tutorial/jndi/newstuff/paged-results.html .

此外,请查看来自 Java 论坛的 JNDI 代码示例 - https://community.oracle.com/thread/1157644?tstart=0 .

希望这对您有所帮助。

关于返回不完整用户列表的 Java Active Directory 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33444107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com