此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring LDAP 3.2.11! |
高级 LDAP 查询
本节介绍了如何将 LDAP 查询与 Spring LDAP 一起使用的各种方法。
LDAP 查询生成器参数
这LdapQueryBuilder
及其关联的类旨在支持可提供给 LDAP 搜索的所有参数。
支持以下参数:
-
base
:指定 LDAP 树中应开始搜索的根 DN。 -
searchScope
:指定搜索应遍历 LDAP 树的深度。 -
attributes
:指定要从搜索中返回的属性。默认值为 all。 -
countLimit
:指定要从搜索中返回的最大条目数。 -
timeLimit
:指定搜索可能需要的最长时间。 -
搜索过滤器:我们正在寻找的条目必须满足的条件。
一LdapQueryBuilder
是通过调用query
method 的LdapQueryBuilder
.它旨在用作 Fluent Builder API,其中首先定义基本参数,然后定义过滤器规范调用。开始后,通过调用where
method 的LdapQueryBuilder
,稍后尝试调用 (例如)base
被拒绝。基本搜索参数是可选的,但至少需要一个过滤器规范调用。
以下查询搜索对象类为Person
:
Person
import static org.springframework.ldap.query.LdapQueryBuilder.query;
...
List<Person> persons = ldapClient.search()
.query(query().where("objectclass").is("person"))
.toList(new PersonAttributesMapper());
The following query searches for all entries with an object class of person
and a cn
(common name) of John Doe
:
Example 2. Search for all entries with object class person
and cn=John Doe
import static org.springframework.ldap.query.LdapQueryBuilder.query;
...
List<Person> persons = ldapClient.search()
.query(query().where("objectclass").is("person").and("cn").is("John Doe"))
.toList(new PersonAttributesMapper());
The following query searches for all entries with an object class of person
and starting at a dc
(domain component) of dc=261consulting,dc=com
:
Example 3. Search for all entries with object class person
starting at dc=261consulting,dc=com
import static org.springframework.ldap.query.LdapQueryBuilder.query;
...
List<Person> persons = ldapClient.search()
.query(query().base("dc=261consulting,dc=com").where("objectclass").is("person"))
.toList(new PersonAttributesMapper());
The following query returns the cn
(common name) attribute for all entries with an object class of person
and starting at a dc
(domain component) of dc=261consulting,dc=com
:
Example 4. Search for all entries with class Person
starting at dc=261consulting,dc=com
, returning only the cn
attribute
import static org.springframework.ldap.query.LdapQueryBuilder.query;
...
Stream<Person> persons = ldapClient.search()
.query(query().base("dc=261consulting,dc=com")
.attributes("cn")
.where("objectclass").is("person")),
.toStream(new PersonAttributesMapper());
The following query uses or
to search for multiple spellings of a common name (cn
):
Example 5. Search with or
criteria
import static org.springframework.ldap.query.LdapQueryBuilder.query;
...
Stream<Person> persons = ldapClient.search()
.query(query().where("objectclass").is("person"),
.and(query().where("cn").is("Doe").or("cn").is("Doo"))
.toStream(new PersonAttributesMapper());
Filter Criteria
The earlier examples demonstrate simple equals conditions in LDAP filters. The LDAP query builder has support for the following criteria types:
-
is
: Specifies an equals (=) condition.
-
gte
: Specifies a greater-than-or-equals (>=) condition.
-
lte
: Specifies a less-than-or-equals (⇐) condition.
-
like
: Specifies a “like” condition where wildcards can be included in the query — for example, where("cn").like("J*hn Doe")
results in the following filter: (cn=J*hn Doe)
.
-
whitespaceWildcardsLike
: Specifies a condition where all whitespace is replaced with wildcards — for example, where("cn").whitespaceWildcardsLike("John Doe")
results in the following filter: (cn=John*Doe)
.
-
isPresent
: Specifies a condition that checks for the presence of an attribute — for example, where("cn").isPresent()
results in the following filter: (cn=*)
.
-
not
: Specifies that the current condition should be negated — for example, where("sn").not().is("Doe)
results in the following filter: (!(sn=Doe))
Hardcoded Filters
There may be occasions when you want to specify a hardcoded filter as input to an LdapQuery
. LdapQueryBuilder
has two methods for this purpose:
-
filter(String hardcodedFilter)
: Uses the specified string as a filter. Note that the specified input string is not touched in any way, meaning that this method is not particularly well suited if you are building filters from user input.
-
filter(String filterFormat, String… params)
: Uses the specified string as input to MessageFormat
, properly encoding the parameters and inserting them at the specified places in the filter string.
-
filter(Filter filter)
: Uses the specified filter.
You cannot mix the hardcoded filter methods with the where
approach described earlier. It is either one or the other. If you specify a filter by using filter()
, you get an exception if you try to call where
afterwards.