[英]How do I configure a single page Google and Azure OAuth2 login in Spring Boot and Spring Security?
[英]Spring Boot Java config with Spring Security: How do I configure to use FilterBasedLdapUserSearch and BindAuthenticator?
我正在从使用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配置的格式错误。 一旦我将其纠正为如下所示,它的效果就很好。 希望这可以帮助其他人使其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")
}
}
解决了这个问题之后,我已经能够添加一个自定义的AuthorityPopulator,以基于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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.