Spring LDAP 抛出 NPE

Spring LDAP throwing a NPE

我正在使用以下代码通过 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)

关于我哪里错了有什么想法吗?

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