active-directory - 用于 LDAPTemplate.ignorePartialResultException 的 Spring Java 配置

标签 active-directory spring-ldap spring-java-config spring-security-ldap

很长的问题,但简而言之,我想知道如何使用 Spring Java Config 设置 org.springframework.ldap.core.LdapTemplate#ignorePartialResultException 标志。

长篇大论的问题是...

我想首先使用我们公司的 Active Directory 进行身份验证(我指的是用户/密码检查),然后进行授权(以便他们有权使用哪些角色)。

我拿了spring ldap guide并进行更改以连接到我们公司的 Active Directory,而不是使用指南的 LDIF 文件。我从调试中知道该程序连接到 Active 目录并正确验证了用户但是在检索权限时我得到以下异常:

Unprocessed Continuation Reference(s); nested exception is javax.naming.PartialResultException

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

org.springframework.ldap.core.LdapTemplate#search(org.springframework.ldap.core.SearchExecutor, org.springframework.ldap.core.NameClassPairCallbackHandler, org.springframework.ldap.core.DirContextProcessor) 

引用我的 WebSecurityConfig 文件:

    @Bean
    public BaseLdapPathContextSource contextSource() throws Exception {
        DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://<ad-host>:389/");

        contextSource.setUserDn(/*principleCN*/);
        contextSource.setPassword(/*principlePassword*/);
        contextSource.setReferral("ignore"); 
        contextSource.afterPropertiesSet();

        return contextSource;
    }

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

最佳答案

据我所知,您可以通过 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());
}

需要注意的是,我没有明确测试过这个,所以您可能需要稍微调整一下您的配置。

关于active-directory - 用于 LDAPTemplate.ignorePartialResultException 的 Spring Java 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524717/

相关文章:

spring ldap 2.0.1 替换已弃用的 OdmManager

java - 某些属性的某些字符的 LDAP 搜索失败

java - 查询用户密码以使用 LDAP 安装应用程序

spring - 当从 weblogic12c(12.1.3) 移动到 weblogic12cR2 (12.2.1) 时,基于注释/java 配置的应用程序开始查找 xml 配置文件

powershell - 尝试从 Get-ADUser 中找出经理名称

arrays - 在 Powershell 中比较数组

Java使用spring bean设置静态字段

javascript - Spring 响应ajax : 403 error

active-directory - 身份服务器和 ADFS

c# - 无法使用 API 更改 Power BI 连接字符串