【问题标题】:Customize LdapAuthoritiesPopulator in configuration在配置中自定义 LdapAuthoritiesPopulator
【发布时间】:2016-12-22 00:50:59
【问题描述】:

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();
    }
}

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

有什么建议吗?

【问题讨论】:

  • 你找到解决办法了吗?

标签: spring spring-security ldap spring-security-ldap


【解决方案1】:

解决方法是在 LdapAuthoritiesPopulator 中设置这个属性并传递给 LdapAuthenticationProvider

参考示例 1:https://www.programcreek.com/java-api-examples/?api=org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator

@豆 public LdapAuthoritiesPopulator authorityPopulator(){

    DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator(
            contextSource(),
            groupSearchBase);

    populator.setGroupSearchFilter("(uniqueMember={0})");
    populator.setGroupRoleAttribute("cn");
    **populator.setSearchSubtree(true);**
    populator.setRolePrefix("");

    return populator;
}

【讨论】:

  • 示例不完整,因为OP使用builder变体进行配置,不允许访问contextSource()
【解决方案2】:

您需要添加如下内容:

final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

在您开始搜索之前。 为什么它被称为“控制”是我无法理解的(一个 LDAP 人),但这就是 Spring 所做的。

-吉姆

【讨论】:

  • 我遇到了与 OP 相同的问题,但您的回答无济于事。我们应该如何将 SearchControl 一直向下传递到那些配置构建器?
猜你喜欢
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 2014-11-15
  • 2014-01-10
相关资源
最近更新 更多