Spring LDAP抛出NPE

时间:2016-08-17 08:54:18

标签: java spring authentication spring-ldap

我使用以下代码使用spring LDAP进行身份验证。

public static void main(String[] args) {
    String username = "kgam";
    String password = "password";

    LdapContextSource ctxSrc = new LdapContextSource();
    ctxSrc.setUrl(URL);
    ctxSrc.setBase(BASE);
    ctxSrc.setUserDn(USERDN);
    ctxSrc.setPassword(PASSWORD);
    LdapTemplate ldapTemplate = new LdapTemplate(ctxSrc);
    AndFilter filter = new AndFilter();
    ldapTemplate.setIgnorePartialResultException(true);
    filter.and(new EqualsFilter("objectClass", "person")).and(new EqualsFilter("sAMAccountName", username));
    System.out.println(ldapTemplate.authenticate(BASE, filter.toString(), password));
}

它会抛出一个带有以下堆栈跟踪的NPE。

Exception in thread "main" java.lang.NullPointerException
    at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:138)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:286)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:236)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:623)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:534)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:461)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:482)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:502)
    at org.springframework.ldap.core.LdapTemplate.authenticate(LdapTemplate.java:1481)
    at org.springframework.ldap.core.LdapTemplate.authenticate(LdapTemplate.java:1398)
    at com.capitalone.dashboard.service.AuthenticationServiceImpl.main(AuthenticationServiceImpl.java:123)

关于我在哪里错误的任何想法?

1 个答案:

答案 0 :(得分:0)

如果在没有Spring帮助的情况下完全配置Spring LDAP,就像在您的示例中一样,您需要在完成配置后在afterPropertiesSet上调用ContextSource