Spring LDAP, настройка деталей подключения в java

Я хочу установить LDAP-соединение для вывода списка всех пользователей из AD. Я успешно выполнил это с информацией, хранящейся в XML

<ldap:context-source
url="ldap://<url>"
base="dc=example,dc=local"
username="<user>@example.local"
password="<pass>" />

Но как я могу установить эту информацию из Java, а не в XML? Пробовал с:

LdapContextSource ctxSrc = new LdapContextSource();
    ctxSrc.setUrl("ldap://<url>");
    ctxSrc.setBase("dc=example,dc=local");
    ctxSrc.setUserDn("<user>@example.local");
    ctxSrc.setPassword("<pass>");
LdapTemplate tmpl = new LdapTemplate(ctxSrc);
setLdapTemplate(tmpl);

Но при беге

List users = (List<User>) ldapTemplate.search(LdapUtils.emptyLdapName(), "(&(objectCategory=person)(objectClass=user))", new UserAttributesMapper());

Я получаю NullPointerExeption. Запустив это без настройки свойств из java (т.е. чтения из xml), все работает нормально


person Alchnemesis    schedule 25.04.2014    source источник
comment
Можете ли вы добавить трассировку стека исключений?   -  person bhdrk    schedule 25.04.2014
comment
Обновлен пост со стеком транса   -  person Alchnemesis    schedule 25.04.2014


Ответы (1)


Пожалуйста, попробуйте это

LdapContextSource ctxSrc = new LdapContextSource();
    ctxSrc.setUrl("ldap://<url>");
    ctxSrc.setBase("dc=example,dc=local");
    ctxSrc.setUserDn("<user>@example.local");
    ctxSrc.setPassword("<pass>");

ctxSrc.afterPropertiesSet(); // this method should be called.

LdapTemplate tmpl = new LdapTemplate(ctxSrc);
setLdapTemplate(tmpl);
person bhdrk    schedule 25.04.2014
comment
Это оно ! ctxSrc.afterPropertiesSet(); нужно было вызвать LdapContextSource, я вызывал его по шаблону :) Спасибо! - person Alchnemesis; 25.04.2014