Java ldap搜索非常慢
我正在使用JNDI连接到LDAP active directory,我想搜索名称中包含搜索字符串的用户,因此我的搜索方法如下:Java ldap搜索非常慢,java,active-directory,ldap,jndi,Java,Active Directory,Ldap,Jndi,我正在使用JNDI连接到LDAP active directory,我想搜索名称中包含搜索字符串的用户,因此我的搜索方法如下: public static List<LDAPUser> searchContactsByName( ExtendedDirContext extendedDirContext, String name) { try { LdapContext ldapContext = extendedDirContext.ge
public static List<LDAPUser> searchContactsByName(
ExtendedDirContext extendedDirContext, String name) {
try {
LdapContext ldapContext = extendedDirContext.getLdapContext();
String searchBaseStr = extendedDirContext.getSearchBase();
String sortKey = LDAPAttributes.NAME;
ldapContext.setRequestControls(new Control[] { new SortControl(
sortKey, Control.CRITICAL) });
SearchControls searchCtls = new SearchControls();
searchCtls.setTimeLimit(1000 * 10);
String returnedAtts[] = { LDAPAttributes.USER_NAME,
LDAPAttributes.NAME };
searchCtls.setReturningAttributes(returnedAtts);
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(&(ObjectCategory=person)(cn=*" + name
+ "*))";
NamingEnumeration<SearchResult> results = ldapContext.search(
searchBaseStr, searchFilter, searchCtls);
List<LDAPUser> users = new ArrayList<LDAPUser>(0);
while (results.hasMoreElements()) {
SearchResult sr = (SearchResult) results.next();
Attributes attrs = sr.getAttributes();
LDAPUser user = new LDAPUser();
user.setName(attrs.get(LDAPAttributes.NAME).toString()
.replace("cn: ", ""));
user.setUserName(attrs.get(LDAPAttributes.USER_NAME).toString()
.replace("sAMAccountName: ", ""));
users.add(user);
}
return users;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
provider.url=ldap://dc.fabrikam.com:389
security.principal=CN=administrator,CN=Users,DC=fabrikam,DC=com
security.credentials=password
search.base=dc=fabrikam,dc=com
为什么搜索要花这么多时间来检索数据?由于我在广告中只有285个联系人,我能做些什么来加快搜索速度吗?您的过滤器:
"(&(ObjectCategory=person)(cn=*" + name + "*))"
这可能是个问题
我建议您下载一个已知的LDAP实用程序(例如ApacheDirectory Studio浏览器),并尝试不同的搜索过滤器,直到找到一个有效的
首先,试试看
"(&(ObjectCategory=person)(cn= + name ))"
解决方案是更改ldapEnv.put(Context.reference,“follow”)代码>到
ldapEnv.put(Context.reference,“忽略”)代码>你说得对
ldapEnv.put(Context.REFERRAL, "ignore")
未收到连接超时的异常。但当我第一次尝试时,我有一种偏执的感觉。在我将LDAP配置端口从389更改为3268后,我没有收到任何异常,构建成功。3268关于LDAP全局目录的端口。例如,Outlook客户端查询全局编录以查找通讯簿信息。如果获得异常引用类型设置,您可以尝试全局编录。您正在执行前缀通配符搜索,您希望得到什么?这在O(n)
时间内运行。@Michael-O,但我在广告中没有太多用户,只有285个。省略第一个*
,看看它是否改变。@Michael-O同样的行为,超过90秒才能获得搜索结果,请查看我如何连接到ldap的后期更新。我发现改变ldapEnv.put(Context.reference,“follow”);
到ldapEnv.put(Context.reference,“ignore”);
使得搜索非常快速是的,这也是我的解释。奇怪!谢谢你
ldapEnv.put(Context.REFERRAL, "ignore")