0

我正在尝试访问 Ldap 目录中的一些数据。为此,我使用 Spring Ldap。我最近发现有一种神奇的方法可以使用它,也就是 DirContextAdapter。但是,当我可以使用老式方式毫无问题地浏览注册表时,在这种情况下我会收到 NullPointerException。

这是我按照文档示例制作的 dao :

@Component
public class DynSubscriberDao {

    @Autowired
    private LdapTemplate ldapTemplate;

    public void create(DynSubscriber subscriber) {
        DirContextAdapter context = new DirContextAdapter(buildDn(subscriber));
        mapToContext(subscriber, context);
        ldapTemplate.bind(context);
    }

    public void update(DynSubscriber subscriber) {
        Name dn = buildDn(subscriber);
        DirContextOperations contextOperations = ldapTemplate.lookupContext(dn);
        mapToContext(subscriber, contextOperations);
        ldapTemplate.modifyAttributes(contextOperations);
    }

    public void delete(DynSubscriber subscriber) {
        ldapTemplate.unbind(buildDn(subscriber));
    }

    public List findByFullName(String fullName) {
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectclass", "DynSubscriber")).and(
                new WhitespaceWildcardsFilter("cn", fullName));
        return ldapTemplate.search(DistinguishedName.EMPTY_PATH,
                filter.encode(), getContextMapper());
    }

    public List findByName(String name) {
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectclass", "DynSubscriber")).and(
                new WhitespaceWildcardsFilter("cn", name));
        return ldapTemplate.search(DistinguishedName.EMPTY_PATH,
                filter.encode(), getContextMapper());
    }

    public List findAll() {
        System.out.println(getContextMapper().toString());
        EqualsFilter filter = new EqualsFilter("objectclass", "DynSubscriber");
        return ldapTemplate.search(DistinguishedName.EMPTY_PATH,
                filter.encode(), getContextMapper());
    }

    protected ContextMapper getContextMapper() {
        return new DynSubscriberContextMapper();
    }

    protected Name buildDn(DynSubscriber subscriber) {
        return buildDn(subscriber.getFullName());
    }

    protected Name buildDn(String fullName) {
        DistinguishedName dn = new DistinguishedName();
        dn.add("cn", fullName);
        return dn;
    }

    protected void mapToContext(DynSubscriber subscriber, DirContextOperations context) {
        context.setAttributeValues("objectclass", new String[] { "top", "person",
                "DynSubscriber" });
        context.setAttributeValue("cn", subscriber.getFullName());
    }

    private static class DynSubscriberContextMapper extends AbstractContextMapper {
        public Object doMapFromContext(DirContextOperations context) {
            DynSubscriber DynSubscriber = new DynSubscriber();
            DynSubscriber.setFullName(context.getStringAttribute("cn"));
        }
    }
}

这是搜索到的对象 DN:cn=toto,dc=dynamease,dc=net ldapTemplate bean 定义如下:

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
    <property name="url" value="ldap://192.168.1.10:10389" />
    <property name="base" value="dc=dynamease,dc=net" />
    <property name="userDn" value="uid=admin,ou=system" />
    <property name="password" value="secret" />
</bean>

现在,DynSubscriberDao.findAll() 抛出上述异常。顺便说一句,我已经检查了类名;)

有任何想法吗 ?

4

0 回答 0