【发布时间】:2016-10-08 01:03:04
【问题描述】:
我正在从使用 XML 的旧版本 Spring Security 迁移到使用 Java 配置而不是旧 XML 配置的 Spring Boot Security。大约一周前,我拥有最新版本的 Spring Boot 1.3.5.RELEASE。
我当前的 XML 代码使用 FilterBasedLdapUserSearch 和 BindAuthenticator 来帮助查找和验证用户。这是必需的,因为 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