1

我已经实现了一个对象工厂来查找 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;
        }
    }

nameCtx.getNameInNamespace() 只返回一个空字符串。

4

3 回答 3

1
String dn = (String) attrs.get("dn").get();

这只会抛出一个NamingException
我不认为专有名称 (DN) 是 LDAP 对象的属性,它更像是 LDAP 世界中的身份密钥。

于 2008-11-27T11:00:08.440 回答
0

也许?

String dn = (String) attrs.get("dn").get();

它应该像其他任何属性一样吗?

于 2008-11-27T08:06:55.560 回答
0

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

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

我使用 spring-ldap 来处理这类东西,并且在其 DirContextAdapter 和 LdapTemplate 类中没有遇到类似的错误。但话又说回来,我总是将它们绑定到特定的命名空间。

于 2009-06-14T15:54:44.230 回答