使用Spring Security和JavaConfig进行身份验证时出现PartialResultException
我目前正在使用SpringBoot创建一个新的web应用程序,并开始集成SpringSecurity进行身份验证。在成功地完成了基于Spring的引导之后,我想将基于JavaConfig的配置指向我的Active Directory实例 我的应用程序现在按预期处理错误凭据,但有效凭据现在会导致错误使用Spring Security和JavaConfig进行身份验证时出现PartialResultException,java,spring-security,spring-ldap,Java,Spring Security,Spring Ldap,我目前正在使用SpringBoot创建一个新的web应用程序,并开始集成SpringSecurity进行身份验证。在成功地完成了基于Spring的引导之后,我想将基于JavaConfig的配置指向我的Active Directory实例 我的应用程序现在按预期处理错误凭据,但有效凭据现在会导致错误 javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name '' 这是一个常
javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name ''
这是一个常见的问题——在很多地方都遇到过这个问题。解决方案似乎是将Context.reference设置为“follow”,但我找不到任何说明如何使用JavaConfig设置该选项的文档。我在这里的唯一选择是恢复到基于XML的配置吗?Spring似乎正在推动开发人员使用JavaConfig,因此如果可能的话,我希望避免将这两种方法混合使用
以下是我的安全配置:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**").permitAll().anyRequest()
.fullyAuthenticated().and().formLogin();
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.userSearchBase("")
.userSearchFilter("(&(cn={0}))").contextSource()
.managerDn("<username>")
.managerPassword("<password>")
.url("ldap://<url>");
}
}
}
@配置
@启用WebMVC安全性
公共类SecurityConfig扩展了WebSecurity配置适配器{
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.authorizeRequests().antMatchers(“/css/**”).permitAll().anyRequest()
.fullyAuthenticated()和().formLogin();
}
@配置
受保护的静态类AuthenticationConfiguration扩展
GlobalAuthenticationConfigurerAdapter{
@凌驾
public void init(AuthenticationManagerBuilder auth)引发异常{
auth.ldapaauthentication()
.userSearchBase(“”)
.userSearchFilter((&(cn={0}))“”
.managerDn(“”)
.managerPassword(“”)
.url(“ldap://”);
}
}
}
我觉得我需要使用LdapContextSource
的一个实例来实现这一点(因为它方便地有一个setReferral
方法),但我在细节上有点纠结。io给了我足够的时间让我继续下去,现在看来我的工作已经开始了
我不清楚我在这里所做的工作是否有任何重大缺陷,但它似乎是有效的,因此希望这将对未来的其他人有所帮助:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**").permitAll().anyRequest()
.fullyAuthenticated().and().formLogin();
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://<url>");
contextSource.setUserDn("<username>");
contextSource.setPassword("<password>");
contextSource.setReferral("follow");
contextSource.afterPropertiesSet();
LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth.ldapAuthentication();
ldapAuthenticationProviderConfigurer
.userSearchFilter("(&(cn={0}))")
.userSearchBase("")
.contextSource(contextSource);
}
}
}
@配置
@启用WebMVC安全性
公共类SecurityConfig扩展了WebSecurity配置适配器{
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.authorizeRequests().antMatchers(“/css/**”).permitAll().anyRequest()
.fullyAuthenticated()和().formLogin();
}
@配置
受保护的静态类AuthenticationConfiguration扩展
GlobalAuthenticationConfigurerAdapter{
@凌驾
public void init(AuthenticationManagerBuilder auth)引发异常{
DefaultSpringSecurityContextSource contextSource=新的DefaultSpringSecurityContextSource(“ldap://”);
contextSource.setUserDn(“”);
contextSource.setPassword(“”);
contextSource.setReferral(“follow”);
contextSource.AfterPropertieSet();
LdapAuthenticationProviderConfigurer LdapAuthenticationProviderConfigurer=auth.ldapAuthentication();
ldapAuthenticationProviderConfigurer
.userSearchFilter((&(cn={0})))
.userSearchBase(“”)
.contextSource(contextSource);
}
}
}