2016-06-07 190 views
0

我正在从使用XML的Spring Security的旧版本迁移到使用Java配置的Spring Boot Security,而不是旧的XML配置。大约一周前,我有最新版本的Spring Boot 1.3.5.RELEASE使用Spring Security进行Spring Boot Java配置:如何配置使用FilterBasedLdapUserSearch和BindAuthenticator?

我目前的XML代码使用FilterBasedLdapUserSearchBindAuthenticator来帮助查找和验证用户。这是必需的,因为LDAP非常复杂,所以标准的基本弹簧安全设置不会找到用户。我的设置让我成功地登录到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") 
    } 

} 

回答

1

解决

答案是比想象的简单,而是采取了一些寻找得到它。

Java配置的格式错误。一旦我将其修正为如下所示,那就太棒了。希望这可以帮助其他人正确地使用LDAP。

请参阅上面的错误格式的Java配置。

以下是更正后的Java配置: @Configuration 保护静态类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") 
    } 
} 

随着上解决了我所基础上,已经能够添加自定义authoritiesPopulator从数据库中获取角色LDAP提交的用户名。要使用自定义权威人口增加以下内容:

@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); 
    } 
} 
相关问题