gpt4 book ai didi

java - Spring Boot Java 配置与 Spring Security : How do I configure to use FilterBasedLdapUserSearch and BindAuthenticator?

转载 作者:行者123 更新时间:2023-12-02 03:33:57 25 4
gpt4 key购买 nike

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

我当前的 XML 代码使用 FilterBasedLdapUserSearchBindAuthenticator 来帮助查找和验证用户。这是必需的,因为 LDAP 非常复杂,因此标准的基本 Spring Security 设置将找不到用户。我的设置让我成功登录 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 配置: @配置 protected 静态类 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);
}
}

关于java - Spring Boot Java 配置与 Spring Security : How do I configure to use FilterBasedLdapUserSearch and BindAuthenticator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37691015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com