java - LDAP 多重或语法

标签 java ldap

我希望这对于任何 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/

相关文章:

java - 方法 : java. util.ArrayList.getAt() 的无签名适用于参数类型:(HashMap) 值:[[:]]

java.util.concurrent 代码审查

perl - LDAP Active Directory 身份验证问题

java - 如何使用 com.novell.ldap 从 LDAP 获取 "supportedControl"

c# - 错误 : EventHandler "btnLogin_Clicked" with correct signature not found in type

java - JUNG库可以根据预定义的属性制作边吗?

java - Android 空指针异常/调用目标异常

java - 与反编译器相比,Java 代码混淆真的有效吗?

go - 如何使用 go-ldap 模块将成员添加到 GroupOfNames 中?

c# - 如何在 C# LDAP 中对用户进行身份验证