【问题标题】:Spring Boot Java config with Spring Security: How do I configure to use FilterBasedLdapUserSearch and BindAuthenticator?带有 Spring Security 的 Spring Boot Java 配置:如何配置为使用 FilterBasedLdapUserSearch 和 BindAuthenticator?
【发布时间】:2016-10-08 01:03:04
【问题描述】:

我正在从使用 XML 的旧版本 Spring Security 迁移到使用 Java 配置而不是旧 XML 配置的 Spring Boot Security。大约一周前,我拥有最新版本的 Spring Boot 1.3.5.RELEASE

我当前的 XML 代码使用 FilterBasedLdapUserSearchBindAuthenticator 来帮助查找和验证用户。这是必需的,因为 LDAP 非常复杂,因此标准的基本 spring 安全设置将找不到用户。我的设置让我成功登录到 LDAP,(我知道这有效,因为如果我更改用户名或密码,我会收到身份验证错误),但这是使用下面的代码并且不返回任何用户数据.我需要从 LDAP 获取用户数据才能知道他们是合法用户。

我在网上搜索了这方面的教程和示例,但没有找到任何有用的东西。那里有很多,但大部分都引用了基本示例,并没有涉及高级 LDAP 设置。

请有人指出我正确的方向吗?有没有解决这个问题的教程或示例?

这是我现有的 LDAP XML:

<beans:bean id="initialDirContextFactory" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <beans:constructor-arg value="ldapIpAddress:port" />
    <beans:constructor-arg value="dc=hostName,dc=com" />
    <beans:property name="userDn" value="userDNHere" />
    <beans:property name="password" value="passwordHere" />
</beans:bean>
<beans:bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
    <beans:constructor-arg>
        <beans:ref local="ldapBindAuthenticator" />
    </beans:constructor-arg>
    <beans:constructor-arg>
        <beans:ref local="ldapAuthoritiesPopulator" />
    </beans:constructor-arg>
</beans:bean>

<beans:bean id="ldapBindAuthenticator" class="org.springframework.security.ldap.authentication.BindAuthenticator">
    <beans:constructor-arg>
        <beans:ref local="initialDirContextFactory" />
    </beans:constructor-arg>
    <beans:property name="userSearch" ref="userSearch" />
</beans:bean>

<beans:bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
    <beans:constructor-arg index="0">
        <beans:value></beans:value>
    </beans:constructor-arg>
    <beans:constructor-arg index="1">
        <beans:value>userNameSearchHere</beans:value>
    </beans:constructor-arg>
    <beans:constructor-arg index="2">
        <beans:ref local="initialDirContextFactory" />
    </beans:constructor-arg>
    <beans:property name="searchSubtree">
        <beans:value>true</beans:value>
    </beans:property>
</beans:bean>

我当前的 Java 配置在这里:

@Configuration
 protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {

     @Autowired
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
         LdapContextSource lcs = new LdapContextSource();
         lcs.setUserDn("userDHHere");
         lcs.setPassword("passwordHere");
         lcs.setUrl("ldapIpAddress:port/dc=hostHere,dc=com");
         lcs.setReferral("follow");
         lcs.afterPropertiesSet();
         auth
            .ldapAuthentication()
                .contextSource(lcs)
                .userSearchBase("ouBaseHere")
                .userSearchFilter("userNameSearchHere")
    }

 }

【问题讨论】:

    标签: java xml spring spring-security spring-boot


    【解决方案1】:

    已解决

    答案比想象的要简单,但需要一些搜索才能得到它。

    Java 配置的格式错误。一旦我将其纠正为如下所示,它的效果就很棒。希望这可以帮助其他人让他们的 LDAP 正常工作。

    有关格式不正确的 Java 配置,请参见上文。

    以下是更正后的 Java 配置: @配置 受保护的静态类 AuthenticationConfiguration 扩展 GlobalAuthenticationConfigurerAdapter {

     @Autowired
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
         LdapContextSource lcs = new LdapContextSource();
         lcs.setUserDn("userDHHere");
         lcs.setPassword("passwordHere");
         lcs.setUrl("ldapIpAddress:port");
         lcs.setReferral("follow");
         lcs.setBase("dc=hostHere,dc=com");
         lcs.afterPropertiesSet();
         auth
            .ldapAuthentication()
                .contextSource(lcs)
                .userSearchBase("ouBaseHere")
                .userSearchFilter("userNameSearchHere")
        }
    }
    

    解决了这个问题后,我已经能够添加一个自定义的 authorityPopulator 来根据 LDAP 提交的用户名从数据库中获取角色。要对以下内容使用自定义 Authorities Populator:

    @Autowired
    CustomAuthoritiesPopulator customAuthoritiesPopulator;
    
     @Autowired
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
         LdapContextSource lcs = new LdapContextSource();
         lcs.setUserDn("userDHHere");
         lcs.setPassword("passwordHere");
         lcs.setUrl("ldapIpAddress:port");
         lcs.setReferral("follow");
         lcs.setBase("dc=hostHere,dc=com");
         lcs.afterPropertiesSet();
         auth
            .ldapAuthentication()
                .contextSource(lcs)
                .userSearchBase("ouBaseHere")
                .userSearchFilter("userNameSearchHere")
                .ldapAuthoritiesPopulator(customAuthoritiesPopulator);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 2014-02-09
      • 1970-01-01
      • 2022-06-12
      • 2015-11-19
      • 2021-06-29
      相关资源
      最近更新 更多