Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring LdapTemplate使用单独的筛选器在多个基础上搜索_Java_Spring_Spring Ldap - Fatal编程技术网

Java Spring LdapTemplate使用单独的筛选器在多个基础上搜索

Java Spring LdapTemplate使用单独的筛选器在多个基础上搜索,java,spring,spring-ldap,Java,Spring,Spring Ldap,在公司Active Directory中具有以下组织结构: DC=foo,DC=bar,DC=com OU=员工 CN=雇员1 CN=雇员2 OU=实习生 CN=1 CN=实习生2 OU=x OU=y OU=z 我需要检索一个列表 具有属性“A”且不具有属性“B”的员工 以及具有属性“B”和不具有属性“A”的实习生 通过将DC=foo,DC=bar,DC=com设置为基来生成SpringLDAP的LdapContextSource,我在LdapTemplate上看不到任何用于设置

在公司Active Directory中具有以下组织结构:

  • DC=foo,DC=bar,DC=com
    • OU=员工
      • CN=雇员1
      • CN=雇员2
    • OU=实习生
      • CN=1
      • CN=实习生2
    • OU=x
    • OU=y
    • OU=z
我需要检索一个列表

具有属性“A”且不具有属性“B”的员工 以及具有属性“B”和不具有属性“A”的实习生

通过将DC=foo,DC=bar,DC=com设置为基来生成SpringLDAP的
LdapContextSource
,我在
LdapTemplate
上看不到任何用于设置具有单个过滤器的多个搜索基的搜索API

下面是一个示例代码,它不返回任何匹配项

@Configuration
public class LdapConfiguration {

    @Autowired
    Environment env;

    @Bean
    public LdapContextSource contextSource () {
        LdapContextSource contextSource= new LdapContextSource();
        contextSource.setUrl(env.getRequiredProperty("ldap.url"));
        contextSource.setBase("DC=foo,DC=bar,DC=com");
        contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
        contextSource.setPassword(env.getRequiredProperty("ldap.password"));
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        return new LdapTemplate(contextSource());        
    }

    private List<Contact> ldapsearch(AndFilter filter) {
    OrFilter orFilter = new OrFilter();
    // EMPLOYEE FILTER
    AndFilter employeesFilter = new AndFilter();
    employeesFilter.and(filter);
    // ou=employees
    employeesFilter.and(new EqualsFilter(DirectoryConstants.OU, DirectoryConstants.EMPLOYEES));
    // A=*
    employeesFilter.and(new PresentFilter(DirectoryConstants.A));
    // (!(B=*))
    employeesFilter.and(new NotPresentFilter(DirectoryConstants.B));
    // INTERN FILTER
    AndFilter internFilter = new AndFilter();
    internFilter.and(filter);
    // ou=interns
    internFilter.and(new EqualsFilter(DirectoryConstants.OU, DirectoryConstants.INTERNS));
    // (!(A=*))
    internFilter.and(new NotPresentFilter(DirectoryConstants.A));
    // (B=*)
    internFilter.and(new PresentFilter(DirectoryConstants.B));

    orFilter.or(employeesFilter);
    orFilter.or(internFilter);

    List<Contact> contacts = null;
    try {
        contacts = ldapTemplate().search(
                "",
                orFilter.encode(),
                new AttributesMapper<Contact>() {
                    public Contact mapFromAttributes(Attributes attrs) throws NamingException {
                        return buildContact(attrs);
                    }
                });
    } catch (Exception e) {
        logger.error("Active directory search failed. " + e.getMessage());
    }
    return contacts;
}
} 
@配置
公共类LdapConfiguration{
@自动连线
环境环境;
@豆子
公共LdapContextSource上下文源(){
LdapContextSource contextSource=新的LdapContextSource();
setUrl(env.getRequiredProperty(“ldap.url”);
setBase(“DC=foo,DC=bar,DC=com”);
setUserDn(env.getRequiredProperty(“ldap.user”);
setPassword(env.getRequiredProperty(“ldap.password”);
返回contextSource;
}
@豆子
公共LdapTemplate LdapTemplate(){
返回新的LdapTemplate(contextSource());
}
私有列表ldapsearch(和筛选器筛选器){
OrFilter OrFilter=新的OrFilter();
//员工过滤器
AndFilter employeesFilter=新建AndFilter();
员工过滤器和(过滤器);
//ou=员工
employeesFilter.and(新的EqualFilter(directoryStants.OU,directoryStants.EMPLOYEES));
//A=*
和(新的PresentFilter(DirectoryConstants.A));
//(!(B=*))
员工过滤器和(新的NotPresentFilter(DirectoryConstants.B));
//内部过滤器
AndFilter internFilter=新建AndFilter();
内部过滤器和(过滤器);
//ou=实习生
和(新的EqualFilter(directoryStants.OU,directoryStants.INTERNS));
//(!(A=*))
和(新的NotPresentFilter(DirectoryConstants.A));
//(B=*)
internFilter.and(新的PresentFilter(DirectoryConstants.B));
或过滤。或(员工过滤);
或(内部过滤器);
列出联系人=空;
试一试{
联系人=ldapTemplate()。搜索(
"",
orFilter.encode(),
新属性映射(){
公共联系人mapFromAttributes(Attributes attrs)引发NamingException{
返回构建联系人(attrs);
}
});
}捕获(例外e){
logger.error(“Active directory搜索失败。”+e.getMessage());
}
返回联系人;
}
} 
我认为上面的过滤器
ou=employees
ou=interest
不应该是过滤器的一部分,而应该是
base
的一部分(ldapTemplate().search()的第一个参数)。但是,我找不到任何API,既不能将多个基设置为
ldapTemplate().search()
,也不能为每个基设置单独的过滤器


关于在单个步骤中执行此查询,您有什么想法吗?

您可以使用LdapQuery.base(directorystants.EMPLOYEES)筛选其OU为directoryStants.EMPLOYEES的项。下面的代码显示查找所有项,其OU为“dev”,名为objectClass的属性为“group”

LdapQuery query = LdapQueryBuilder.query()
            .base("ou=dev")
            .where("objectClass").is("group");

    return ldapTemplate.search(query, new AttributesMapper<String>() {
        @Override
        public String mapFromAttributes(Attributes attributes) throws NamingException {
            return (String) attributes.get("cn").get();
        }
    });
LdapQuery query=LdapQueryBuilder.query()
.base(“ou=dev”)
其中(“对象类”)为(“集团”);
返回ldapTemplate.search(查询,新属性映射(){
@凌驾
公共字符串mapFromAttributes(Attributes属性)引发NamingException{
返回(字符串)attributes.get(“cn”).get();
}
});

尽管Spring LDAP的配置很简单,但我还是按照@TobySpeight的评论插入了一个示例代码。要么从树的更高一级的公共祖先节点开始搜索,要么在两个单独的搜索中开始搜索,并在后面加入结果。感谢@marthursson的评论。我已经尝试了这两种方法,显然这两种方法都增加了查询时间。然而,在我的例子中,由于超载和结构不太好的公司广告结构,执行两个单独的查询会更快。这就是我目前应用程序中的逻辑。