按多个属性搜索 LDAP 模板

Nim*_*ish 2 spring spring-ldap

尝试使用 userid、emailid、firstname、lastname、GUID 等搜索用户详细信息...将来需要添加更多值

应该使用所有不为空的属性来执行搜索。在网上找到这段代码*

字符串过滤器 = "(&(sn=YourName)(mail=*))";

* 是否有任何其他预定义模板或类似的模板来进行搜索,而不是直接将值指定为 Null 或为每个属性使用 if else 语句的更优化方法?所有值都必须传递给该方法,非空值必须用于使用 LDAP 进行搜索。任何事物?请帮忙。

Avi*_*vis 5

您可以在运行时有效地使用过滤器来指定用于搜索的内容以及不依赖于某些规则或对属性的 NULL 验证的内容。请在 ldapTemplate 中找到使用过滤器获取人名的示例代码:-

public static final String BASE_DN = "dc=xxx,dc=yyy";
private LdapTemplate ldapTemplate ;
public List getPersonNames() { 
    String cn = "phil more";
    String sn = "more";
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("objectclass", "person"));
    filter.and(new EqualsFilter("sn", sn));
    filter.and(new WhitespaceWildcardsFilter("cn", cn));
    return ldapTemplate.search(
       BASE_DN, 
       filter.encode(),
       new AttributesMapper() {
          public Object mapFromAttributes(Attributes attrs)
             throws NamingException {
             return attrs.get("cn").get();
          }
       });
 }
Run Code Online (Sandbox Code Playgroud)

顾名思义,AndFilters 将查找中使用的所有单个过滤器连接起来,例如 EqualFilter,它检查属性的相等性,而 WhitespaceWildcardsFilter 执行通配符搜索。所以这里就像我们得到了 cn = phil 更多,它反过来*phil*more*用于搜索。