LDAPTemplate.ignorePartialResultException的Spring Java配置

use*_*555 2 active-directory spring-ldap spring-security-ldap spring-java-config

应用程序学问题很长,但总之我想知道如何org.springframework.ldap.core.LdapTemplate#ignorePartialResultException使用Spring Java Config 设置标志。

长期存在的问题是...

我想使用我们公司的Active Directory进行身份验证(这意味着用户/密码检查),然后再进行授权(因此他们有权使用哪些角色)。

我采用了春季ldap指南,并进行了更改以连接到我们公司的Active Directory,而不是使用指南的LDIF文件。通过调试,我知道程序连接到Active Directory并正确验证了用户身份,但是在检索权限时,出现以下异常:

Unprocessed Continuation Reference(s); nested exception is javax.naming.PartialResultException
Run Code Online (Sandbox Code Playgroud)

从谷歌搜索中,我看到这是一个常见的LDAP / ActiveDirectory问题,我需要设置标志以忽略引用。我遵循了这个SO问题中的示例。但是,我仍然遇到异常,通过调试,我可以发现在为用户搜索角色时,以下方法中发生了错误。

org.springframework.ldap.core.LdapTemplate#search(org.springframework.ldap.core.SearchExecutor, org.springframework.ldap.core.NameClassPairCallbackHandler, org.springframework.ldap.core.DirContextProcessor) 
Run Code Online (Sandbox Code Playgroud)

作为参考,我的WebSecurityConfig文件:

Unprocessed Continuation Reference(s); nested exception is javax.naming.PartialResultException
Run Code Online (Sandbox Code Playgroud)

DB5*_*DB5 6

据我所知,您可以通过来实现此目标DefaultLdapAuthoritiesPopulator,它有一种setIgnorePartialResultException(boolean)方法可以实现这一目标。

因此,在您的配置类中,您需要按照以下几行进行操作:

@Bean
public LdapAuthoritiesPopulator ldapAuthoritiesPopulator() throws Exception {
    DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator(contextSource(), "dc=<company>,dc=local");
    populator.setIgnorePartialResultException(true);
    return populator;
}

@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
    auth.ldapAuthentication()
            .userSearchFilter("(&(sAMAccountName={0})(objectclass=user))")
            .userSearchBase("dc=<company>,dc=local")
            .contextSource(contextSource())
            .ldapAuthoritiesPopulator(ldapAuthoritiesPopulator());
}
Run Code Online (Sandbox Code Playgroud)

需要注意的是,我没有对此进行显式测试,因此您可能需要稍微修改一下配置。