ldapTemplate.search在searchControles上返回空列表

时间:2017-03-08 06:39:50

标签: java ldap spring-ldap

我正在尝试获取用户的详细信息。提供SearchControls时,它返回一个空列表。

@Override
public User getUserDetails(String userName) {
    SearchControls ctls= new SearchControls();
    String [] attrs = {"mail"};
    ctls.setReturningAttributes(attrs);

    log.info("executing {getUserDetails}");
    List<User> list = ldapTemplate.search("","(&(objectClass=person)(mail=josh.nail@gmail.com))",ctls, new UserAttributesMapper());
    if (list != null && !list.isEmpty()) {
        return list.get(0);
    }
    return null;
}

如果我们打电话

ldapTemplate.search("","(&(objectClass=person)(mail=josh.nail@gmail.com))", new UserAttributesMapper());

没有searchcontroles,它会获取用户详细信息。设置ReturningAttributes时是否有任何特定的标准?

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您未指定SearchControls,则LdapTemplate.search()将使用其defaultSearchScope,默认情况下为SearchControl#SUBTREE_SCOPE

如果您传递了自己的SearchControls个对象,LdapTemplate.search()将使用searchScope中定义的SearchControls

SearchControls ctls= new SearchControls();但是会​​将searchScope设置为SearchControls#ONELEVEL_SCOPE,因此您的搜索只会查找作为搜索库的直接子项的条目。

简而言之,您通常希望创建一个SearchControls对象,如下所示:

SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String [] attrs = {"mail"};
ctls.setReturningAttributes(attrs);