简体   繁体   English

当对象名称中包含单引号或双引号时,如何为Java ldap搜索api构造搜索过滤器

[英]How to construct search filter for a java ldap search api ,when object name has single or double quote in it

I have an object, cn=abc"and'def in the directory. I am using the Java search API: 我在目录中有一个对象cn=abc"and'def 。我正在使用Java搜索API:

public LDAPSearchResults search(java.lang.String base,
    int scope,
    java.lang.String filter,
    java.lang.String[] attrs,
    boolean typesOnly,
    LDAPSearchConstraints cons)
    throws LDAPException

I tried giving the search filter as abc"and'def and also as abc\\"and\\'def . 我尝试将搜索过滤器指定为abc"and'def abc\\"and\\'def Both return: 两者都返回:

Bad search filter 错误的搜索过滤器

Please help me as to how to construct the search filter when the object name has single or double quote in it. 当对象名称中包含单引号或双引号时,请帮助我了解如何构造搜索过滤器。

The entire LDAP search filter must be a valid UTF-8 string. 整个LDAP搜索过滤器必须是有效的UTF-8字符串。 There five (5) values that, should they appear in a search filter, must be escaped using a backslash \\ and the two-digit hexadecimal code for the character being escaped. 如果有五(5)个值出现在搜索过滤器中,则必须使用反斜杠\\和要转义的字符的两位十六进制代码进行转义。 The values that must be escaped are * , ( , ) , \\ , and the null byte 0 ; 必须转义的值为*()\\和空字节0 therefore the " and the ' are legal and valid characters in the search filter. In a language like Java that encloses a string literal between " characters, the " character appearing as part of string literal must be escaped. 因此, "'在搜索过滤器合法,有效字符。在Java等语言包围字符串字面之间"字, "字符显示为字符串文字的一部分,必须进行转义。

In one example, you list the filter with a backslash \\ character in the filter. 在一个示例中,您在过滤器中列出了带有反斜杠\\字符的过滤器。 A backslash must be escaped in the filter using a backslash and the hexadecimal code for backslash, for example, "(cn=abc\\5c\\"and'def)'" . In the other example, you list as the filter "(cn=abc"and'def)" which is in fact a legal search filter - ignoring the fact that the inner " is not escaped as it must be for compilation. 必须在过滤器中使用反斜杠和反斜杠的十六进制代码对反斜杠进行转义,例如"(cn=abc\\5c\\"and'def)'" 。在另一个示例中,您将过滤器"(cn=abc"and'def)"实际上是一个合法的搜索过滤器-忽略了内部"不会被转义的事实,因为它必须用于编译。

By way of example, I created an object in a directory at my localhost listening on port 1389 with prefix or naming context dc=example,dc=com using the following LDIF: 举例来说,我在本地主机的目录中创建了一个对象,并使用以下LDIF使用前缀或命名上下文dc=example,dc=com在端口1389上进行侦听:

dn: cn=abc"and'def,dc=example,dc=com
objectClass: top
objectClass: person
cn: abc"and'def
sn: whatever

I wrote a Java class to search for the entry, throwing an assertion error if it should not be found: 我编写了一个Java类来搜索条目,如果不应找到则抛出一个断言错误:

import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SearchScope;
import com.unboundid.ldap.sdk.SearchResult;

public final class BSFilter {
  public static void main(String... args) {
    try {
      Filter searchFilter =
        Filter.create("cn=abc\"and'def");
      LDAPConnection connection =
        new LDAPConnection("localhost",1389);
      SearchResult searchResult =
        connection.search("dc=example,dc=com",SearchScope.ONE,
                          searchFilter,"1.1");
      assert(searchResult.getSearchEntries().size() == 0);
    } catch(LDAPException lex) {
      lex.printStackTrace();
      return;
    }
  }
}

This class compiles and throws an assertion error as expected because the entry for which it searches does in fact exist. 此类会按预期进行编译并引发断言错误,因为实际上它搜索的条目确实存在。 See RFC 4515 for information regarding the search filter. 有关搜索过滤器的信息,请参阅RFC 4515 The LDAPSDK used is the excellent SDK from UnboundID. 所使用的LDAPSDK是UnboundID提供的出色的SDK Notice that the " character is escaped in the filter so that the class will compile, but that has nothing to do with the filter text itself. 请注意,在过滤器中转义了"字符,以便类可以编译,但这与过滤器文本本身无关。

Use the force of the filter to handle escaping for your. 用过滤器的力为您处理逃逸。 Something like: 就像是:

"(&(objectClass=user)(cn={0}))"

I use JNDI and one of the search() overloads that take a `filterArgs' argument. 我使用JNDI以及带有`filterArgs'参数的search()重载之一。 Does all the escaping required for you. 是否为您进行了所有转义操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM