【问题标题】:LDAP template search by multiple attributes通过多个属性进行 LDAP 模板搜索
【发布时间】:2015-09-25 19:33:01
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: spring spring-ldap


    【解决方案1】:

    您可以在运行时有效地使用过滤器来指定用于搜索的内容以及不依赖于某些规则或您对属性的 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();
              }
           });
     }
    

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

    【讨论】:

    • 感谢 Avis...过滤器正是我所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    相关资源
    最近更新 更多