我们安装了LDAP(Directory Server),其中包含数百万用户。当新用户选择用户名时,我们需要在LDAP中验证用户名,并且需要超过一分钟的时间,这非常慢。此外,当我们创建新用户时,它会同时发生。我已经用java编写了代码,它在5-10,000个用户中运行良好。因此,任何改善搜索时间的想法都会有所帮助。
问题是所有用户都在同一个树中 - 没有组或组织,这是要求,我们无法更改它。我在命令行中尝试使用ldapsearch,我立即得到了结果。我用了
"ldapsearch -D "" -w "" -h -p -b "ou=company,dc=abc,dc=com" uid=user1"
代码如下:
public String userCheck(String name){
String text = "";
Hashtable<String, String> environment = new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, "ldap://xxxxxxxxxx:1389");
environment.put(Context.SECURITY_AUTHENTICATION, ""); ////////these values are all defined
environment.put(Context.SECURITY_PRINCIPAL, "" );
environment.put(Context.SECURITY_CREDENTIALS, "");
try
{
//DirContext context = new InitialDirContext(environment);
String searchBase = "ou=company,dc=abc,dc=com"; ////////////////////////This is where all the 2 million users are stored
String FILTER = "(uid="+name+")";
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answer = context.search(searchBase, FILTER, ctls);
SearchResult result = answer.next();
javax.naming.directory.Attribute email = result.getAttributes().get("mail");
javax.naming.directory.Attribute cn = result.getAttributes().get("cn");
text = "Taken" ;
context.close();
}
catch (Exception a)
{
text = "available" ;
}
return text;
}
感谢。
编辑:我能够更快地完成任务。我重新创建了索引并重新生成它们,现在工作正常。再次感谢您的帮助。