我希望这对于任何 LDAP 专家来说都是一个简单的问题。
我正在使用 java、SearchDirContext 和字符串生成器来组合一个如下所示的查询:
(|(givenName=smith*)(sn=smith*)(middleName=smith*)(mail=smith*)(电话号码=smith*)(buildingName=smith*)(部门=smith*)(major=smith*)(minor=smith*))
。这个想法是允许用户使用单个字符串进行搜索并获得与任何这些属性匹配的结果。
查询成功完成,但结果不准确。例如,如果我搜索自己(我知道我的记录存在)...
- 按姓氏我没有得到任何结果
- 按名字(应该有数百个结果),我得到一个小子集 (9),其中不包括我的条目。
我想首先消除查询出现问题的任何可能性,如果您想了解代码执行的更多信息/代码片段,请告诉我,我可以提供。
另请记住,我是正确做事的强烈倡导者,并且愿意修改我的代码的任何部分以提高效率。
--------------------(编辑)所以语法是正确的......(编辑)--------------------
这是我的查询的一些代码,也许这可以确定我的结果是否被截断。
try {
context = ldapPooler.getContext(); // Returns a custom SearchDirContext object wrapping a javax.naming.DirContext.
SearchControls controls = new SearchControls();
controls.setCountLimit(maxResultCount);
Integer resultCount = 0;
// They try block is from an example found at
// http://www.java2s.com/Code/Java/JNDI-LDAP/howtoperformasearchandlimitthenumberofresultsreturned.htm
// The goal was to limit the results.
try {
logger.debug("Finished w/the search string: " + ldapSearchString);
@SuppressWarnings("unchecked")
NamingEnumeration<SearchResult> result = context.search("ou=People", ldapSearchString, controls);
// SearchDirContext.search simply calls DirContext.search with the passed attributes.
while (result.hasMore()) {
searchResults.add(result.next());
resultCount++;
}
logger.debug("Found results: " + resultCount);
} catch (LimitExceededException lee) {
logger.debug("Caught LimitExceededException w/resultCount: " + resultCount);
if (resultCount == maxResultCount) {
logger.debug("Found " + resultCount + " results.");
} else {
logger.debug("In the else....not throwing an exception. Found " + resultCount + " results.");
}
} finally {
context.close();
}
} catch (NamingException ne) {
logger.error("Caught a NamingException while gettingContactCardsBySearchString(" + searchString + ")");
throw new LdapLookupFailedException(ne);
} catch (Exception e) {
logger.error("Caught Exception while gettingContactCardsBySearchString(" + searchString + ")");
throw new LdapLookupFailedException(e);
}
最佳答案
根据 RFC 4515,您的过滤器语法是正确的。我建议您不要将测试值放入搜索字符串中。使用 {0}、{1} 表示法并将值作为参数提供给 search()。当您获得的搜索结果少于预期时,您可能会遇到分页搜索结果。我将使用独立的 LDAP 客户端(例如 JXplorer)测试您的过滤器。
关于java - LDAP 多重或语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6430597/