我正在使用 Apache Directory Studio 使用 Spring Security 应用程序构建 LDAP。
LDIF 文件:
version: 1
dn: o=mojo
objectClass: organization
objectClass: top
o: mojo
dn: ou=users,o=mojo
objectClass: organizationalUnit
objectClass: top
ou: users
dn: ou=groups,o=mojo
objectClass: organizationalUnit
objectClass: top
ou: groups
dn: cn=Ramesh Kotha,ou=users,o=mojo
objectClass: organizationalPerson
objectClass: person
objectClass: inetOrgPerson
objectClass: top
cn: Ramesh Kotha
sn: Kotha
uid: ramesh
userPassword:: cmFtZXNo
dn: cn=USER,ou=groups,o=mojo
objectClass: groupOfUniqueNames
objectClass: top
cn: USER
uniqueMember: cn=Ramesh Kotha,ou=users,o=mojo
dn: cn=ADMIN,ou=groups,o=mojo
objectClass: groupOfUniqueNames
objectClass: top
cn: ADMIN
uniqueMember: cn=Ramesh Kotha,ou=users,o=mojo
我的测试课是:
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.apache.log4j.Logger;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.filter.EqualsFilter;
public class LdapTest {
static Logger logger = Logger.getLogger("LdapTest");
public static void main(String[] args) throws Exception {
LdapContextSource ctxSrc = new LdapContextSource();
ctxSrc.setUrl("ldap://localhost:10389/o=mojo");
ctxSrc.setBase("o=mojo");
ctxSrc.setUserDn("uid=admin,ou=system");
ctxSrc.setPassword("secret");
ctxSrc.afterPropertiesSet();
LdapTemplate tmpl = new LdapTemplate(ctxSrc);
PersonDao dao = new PersonDao(tmpl);
dao.getAllPersonNames();
}
public static class PersonDao {
private LdapTemplate ldapTemplate;
public PersonDao(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public List getAllPersonNames() throws Exception {
logger.info("getAllPersonNames called : ");
EqualsFilter filter = new EqualsFilter("objectclass", "person");
return ldapTemplate.search(DistinguishedName.EMPTY_PATH,
filter.encode(),
new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs) throws NamingException {
return attrs.get("cn").get();
}
});
}
}
}
错误
Exception in thread "main" org.springframework.ldap.NameNotFoundException: [LDAP: error code 32 - NO_SUCH_OBJECT: failed for SearchRequest
baseDn : '2.5.4.10=mojo/o=mojo'
filter : '(2.5.4.0=person)'
scope : whole subtree
typesOnly : false
Size Limit : no limit
Time Limit : no limit
Deref Aliases : deref Always
attributes :
: Cannot find a partition for 2.5.4.10=mojo/o=mojo]; nested exception is javax.naming.NameNotFoundException: [LDAP: error code 32 - NO_SUCH_OBJECT: failed for SearchRequest
baseDn : '2.5.4.10=mojo/o=mojo'
filter : '(2.5.4.0=person)'
scope : whole subtree
typesOnly : false
Size Limit : no limit
Time Limit : no limit
Deref Aliases : deref Always
attributes :
: Cannot find a partition for 2.5.4.10=mojo/o=mojo]; remaining name '/'
at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:174)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:306)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:237)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:588)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:546)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:401)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:421)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:441)
at com.onlineexam.util.LdapTest$PersonDao.getAllPersonNames(LdapTest.java:49)
at com.onlineexam.util.LdapTest.main(LdapTest.java:30)
Caused by: javax.naming.NameNotFoundException: [LDAP: error code 32 - NO_SUCH_OBJECT: failed for SearchRequest
baseDn : '2.5.4.10=mojo/o=mojo'
filter : '(2.5.4.0=person)'
scope : whole subtree
typesOnly : false
Size Limit : no limit
Time Limit : no limit
Deref Aliases : deref Always
attributes :
: Cannot find a partition for 2.5.4.10=mojo/o=mojo]; remaining name '/'
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3030)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2951)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2758)
at com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1812)
at com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1735)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(ComponentDirContext.java:368)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:338)
at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:257)
at org.springframework.ldap.core.LdapTemplate$3.executeSearch(LdapTemplate.java:231)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:293)
... 8 more