Spring LDAP模板多属性搜索

Spring LDAP模板多属性搜索,spring,spring-ldap,Spring,Spring Ldap,试图通过使用userid、emailid、firstname、lastname、GUID等搜索用户详细信息…将来需要添加更多的值 应使用所有非空属性执行搜索。 在网上找到这段代码* String filter=“(&(sn=YourName)(mail=*)” * 是否有其他预定义的模板或类似的模板来执行搜索,而不直接将值指定为Null或为每个属性使用if-else语句,这是一种更优化的方法?所有值都必须传递给该方法,而那些非null的值必须用于使用LDAP进行搜索。任何东西请提供帮助。您可以在

试图通过使用userid、emailid、firstname、lastname、GUID等搜索用户详细信息…将来需要添加更多的值

应使用所有非空属性执行搜索。 在网上找到这段代码*

String filter=“(&(sn=YourName)(mail=*)”

*
是否有其他预定义的模板或类似的模板来执行搜索,而不直接将值指定为Null或为每个属性使用if-else语句,这是一种更优化的方法?所有值都必须传递给该方法,而那些非null的值必须用于使用LDAP进行搜索。任何东西请提供帮助。

您可以在运行时有效地使用过滤器来指定用于搜索的内容,以及不依赖于某些规则或属性的空验证的内容。请在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*
进行搜索。

您可以在运行时有效地使用过滤器来指定用于搜索的内容以及不依赖于某些规则或属性的空验证的内容。请在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…过滤器正是我需要的Hanks Avis…过滤器正是我需要的