使用javax.naming.spi.DirObjectFactory从LDAP查找对象

使用javax.naming.spi.DirObjectFactory从LDAP查找对象,java,jndi,Java,Jndi,我已经实现了一个对象工厂来查找LDAP对象,但是提供的上下文没有从LDAP返回DN(通过nameCtx.getNameInNamespace())。我在某种程度上做错了吗 public class LdapPersonFactory implements DirObjectFactory { @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx,

我已经实现了一个对象工厂来查找LDAP对象,但是提供的上下文没有从LDAP返回DN(通过nameCtx.getNameInNamespace())。我在某种程度上做错了吗

public class LdapPersonFactory implements DirObjectFactory {
        @Override
        public Object getObjectInstance(Object obj, Name name, Context nameCtx,
                Hashtable<?, ?> environment, Attributes attrs) throws Exception {
            if (attrs == null)
                return null;
            Attribute oc = attrs.get("objectclass");
            if (oc != null && oc.contains("inetOrgPerson")) {
                String surname = (String) attrs.get("sn").get();
                String givenName = (String) attrs.get("givenname").get();
                String dn = nameCtx.getNameInNamespace();
                return new LdapPerson(dn, givenName, surname);
            }
            return null;
        }
    }
公共类LdapPersonFactory实现DirObjectFactory{
@凌驾
公共对象getObjectInstance(对象obj、名称、上下文名称CTX、,
哈希表环境、属性(属性属性)引发异常{
if(attrs==null)
返回null;
属性oc=attrs.get(“objectclass”);
如果(oc!=null&&oc.contains(“inetOrgPerson”)){
字符串姓氏=(字符串)attrs.get(“sn”).get();
字符串givenName=(字符串)attrs.get(“givenName”).get();
字符串dn=nameCtx.getNameInNamespace();
返回新的LdapPerson(dn、givenName、姓氏);
}
返回null;
}
}
nameCtx.getNameInNamespace()只返回一个空字符串。

可能吗

String dn = (String) attrs.get("dn").get();
它应该是一个属性,就像其他属性一样?

可能吧

String dn = (String) attrs.get("dn").get();
String dn = (String) attrs.get("dn").get();
它应该是一个属性,就像其他属性一样

String dn = (String) attrs.get("dn").get();
这只会抛出一个
NamingException

我不认为可分辨名称(DN)是LDAP对象的属性,它更像是LDAP世界中的标识键

这只会抛出一个
NamingException


我不认为可分辨名称(DN)是LDAP对象的一个属性,它更像是LDAP世界中的一个标识键。

可能是您的上下文指向“根节点”或它被称为什么。即,具有顶级名称空间作为其子级的节点

我想也可能是在调用getNameInNamespace时上下文没有绑定,尽管我希望这会引发异常


我使用SpringLDAP来实现这类功能,并且没有在DirContextAdapter和LdapTemplate类中遇到过类似的错误。但是,我总是将它们绑定到一个特定的名称空间。

可能是您的上下文指向“根节点”或它被称为什么。即,具有顶级名称空间作为其子级的节点

我想也可能是在调用getNameInNamespace时上下文没有绑定,尽管我希望这会引发异常

我使用SpringLDAP来实现这类功能,并且没有在DirContextAdapter和LdapTemplate类中遇到过类似的错误。但是,我总是将它们绑定到特定的名称空间