在配置中自定义 LdapAuthoritiesPopulator

Jar*_*son 5 spring ldap spring-security spring-security-ldap

DefaultLdapAuthoritiesPopulator 设置搜索范围为“ONE_LEVEL”,但我需要搜索“SUBSCOPE”以获取用户所属组的列表。

我一直遵循“配置”风格的 Spring 设置(代码,而不是 XML)。虽然有大量关于如何在 XML 中配置自定义 LdapAuthoritiesPopulator 的示例,但我对如何在代码中执行此操作感到困惑。

这是我到目前为止所拥有的:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

      @Autowired
      public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.ldapAuthentication()
              .contextSource().url("ldap://ldap.company.org/")
              .and()
                  .userSearchBase("o=company.org,c=us")
                  .userSearchFilter("(uid={0})")
                  .groupSearchBase("o=company.org,c=us")
                  .groupSearchFilter("(&(objectClass=groupOfUniqueNames)(uniqueMember={0}))");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().and().authorizeRequests()
            .antMatchers("/api/**").authenticated()
            .anyRequest().permitAll();
    }
}
Run Code Online (Sandbox Code Playgroud)

缺少的是我需要能够在 DefaultLdapAuthoritiesPopulator 上设置搜索范围。该类本身公开了“setSearchSubtree”方法,但 LdapAuthenticationProviderConfigurer 不提供配置它的方法。

有什么建议么?

jwi*_*eke 0

您需要添加类似以下内容:

final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
Run Code Online (Sandbox Code Playgroud)

在您开始搜索之前。为什么它被称为“控件”超出了我(LDAP 人员)的范围,但这就是 Spring 所做的。

-吉姆