Java 带动态基本DN的SpringLDAP
我有以下LDAP方案: 每个子树都包含组织单元团队。我想从特定子树中查找所有团队。为此,我使用Java 带动态基本DN的SpringLDAP,java,spring,ldap,openldap,spring-ldap,Java,Spring,Ldap,Openldap,Spring Ldap,我有以下LDAP方案: 每个子树都包含组织单元团队。我想从特定子树中查找所有团队。为此,我使用LdapTemplateclass和findAll()方法 ldapTemplate.findAll(Team.class); 当我在LdapContextSource中将base设置为dc=global,dc=id,dc=pl时,它会从全局子树返回我的团队。当我将base更改为dc=id,dc=pl时,它将从所有子树返回我的团队 问题是我想使用DynamicBase,从特定的子树中查找团队。我尝试
LdapTemplate
class和findAll()
方法
ldapTemplate.findAll(Team.class);
当我在LdapContextSource
中将base设置为dc=global,dc=id,dc=pl
时,它会从全局子树返回我的团队。当我将base更改为dc=id,dc=pl时,它将从所有子树返回我的团队
问题是我想使用DynamicBase,从特定的子树中查找团队。我尝试了多种方法来实现这一点,但没有一种方法能给我带来效果
方法1:查找
Name nameBase = LdapUtils.newLdapName("dc=global");
return ldapTemplate.find(query().base(nameBase).where("ou").is("team"), Team.class);
返回空列表
方法2:findAll
Name nameBase = LdapUtils.newLdapName("dc=global");
SearchControls searchControls = new SearchControls();
return ldapTemplate.findAll(nameBase, searchControls, Team.class);
返回空列表
起初,它看起来工作正常,因为当我将子树名称更改为不存在的名称时,我得到了javax.naming.NameNotFoundException:[LDAP:错误代码32-没有这样的对象]
任何关于我为什么在这段代码中得到正确结果的想法:
LdapContextSource contextSource = new LdapContextSource();
contextSource.setBase("dc=global,dc=id,dc=pl");
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
return ldapTemplate.findAll(Team.class);
和此列表中的空列表:
LdapContextSource contextSource = new LdapContextSource();
contextSource.setBase("dc=id,dc=pl");
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
Name nameBase = LdapUtils.newLdapName("dc=global");
SearchControls searchControls = new SearchControls();
return ldapTemplate.findAll(nameBase, searchControls, Team.class);
我使用SpringLDAP核心2.0.3,我找到了解决方案
首先
为搜索控件添加适当的作用域
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
return ldapTemplate.findAll(base, searchControls, Team.class);
秒
Name nameBase = LdapUtils.newLdapName("dc=global");
return ldapTemplate.find(query().base(nameBase).where("ou").is("team"), Team.class);
更改查询参数以检查是否存在cn
return ldapTemplate.find(query().base(base).where("cn").isPresent(), Team.class);