首页 > 解决方案 > 使用 Spring 进行 LDAP 身份验证 - 获取 PartialResultException

问题描述

我正在关注这篇关于使用 Spring 设置 LDAP 身份验证的文章
现在我可以登录到应用程序,但我得到了这个异常:

Unprocessed Continuation Reference(s); nested exception is javax.naming.PartialResultException: 
Unprocessed Continuation Reference(s); remaining name 'DC=XEROX,DC=AD,DC=XEROX,DC=com'
Caused by: javax.naming.PartialResultException: Unprocessed Continuation Reference(s)
    at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2917) ~[na:1.8.0_144]
    at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2891) ~[na:1.8.0_144]
    at com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1846) ~[na:1.8.0_144]
    at com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1769) ~[na:1.8.0_144]

根据我阅读的其他文章,我需要设置要关注的推荐,setReferral("follow");

但我不确定将其添加到此代码的何处:

String completeUrl = new StringBuffer(this.url).append(":")
                    .append(this.port)
                    .append("/")
                    .append(this.contextRoot)
                    .toString();

            auth.ldapAuthentication()
                    .userSearchFilter(userSearchFilter)
                    .userDnPatterns(userDnPatterns)
                    .contextSource()
                    .url(completeUrl)
                    .managerDn(managerDn)
                    .managerPassword(managerPassword);

标签: springldap

解决方案


您应该创建自己的 contextSource,例如:

@Bean("internalLdapContextSource")
public LdapContextSource getLdapContextSource() {
    String ldapUrl = env.getProperty("ldap.server");
    String managerDn = env.getProperty("ldap.manager.distinguished.name");
    String managerPassword = env.getProperty("ldap.manager.password");
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl(ldapUrl);
    contextSource.setUserDn(managerDn);
    contextSource.setPassword(managerPassword);
    Map<String, Object> baseEnvironmentProperties = new HashMap<>();
    baseEnvironmentProperties.put("java.naming.referral", "follow");
    contextSource.setBaseEnvironmentProperties(baseEnvironmentProperties);
    return contextSource;
}

您可以使用示例中所示的setBaseEnvironmentProperties方法或setReferral(两者都可以正常工作)。

最后使用 .contextSource(getLdapContextSource())


推荐阅读