我正在从使用XML的Spring Security的旧版本迁移到使用Java配置的Spring Boot Security,而不是旧的XML配置。大约一周前,我有最新版本的Spring Boot 1.3.5.RELEASE
。使用Spring Security进行Spring Boot Java配置:如何配置使用FilterBasedLdapUserSearch和BindAuthenticator?
我目前的XML代码使用FilterBasedLdapUserSearch
和BindAuthenticator
来帮助查找和验证用户。这是必需的,因为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")
}
}