环境:
- Spring 4.1.6
- Spring 安全 4.0.1
我有 2 个身份验证提供程序 - 一个访问 ActiveDirectory,另一个访问我创建的自定义数据库提供程序。以任一环境中的用户身份登录都可以正常工作。用户已通过身份验证,应用程序将继续。
但是,当输入无效用户并且提供商都无法进行身份验证时,我会在页面上收到此异常:
java.lang.StackOverflowError
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:393)
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:394)
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:394)
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:394)
这是我的 WebSecurityConfigurerAdapter 配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.formLogin().loginPage("/login").failureUrl("/login?error").defaultSuccessUrl("/overview").permitAll()
.and()
.logout().logoutSuccessUrl("/login?logout").permitAll()
.and()
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/favicon.ico").permitAll()
.antMatchers("/**").hasRole("AUTH");
}
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder
.authenticationProvider(activeDirectoryLdapAuthenticationProvider())
.userDetailsService(userDetailsService());
authManagerBuilder
.authenticationProvider(databaseAuthenticationProvider())
.userDetailsService(userDetailsService());
}
@Bean
public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(DOMAIN, URL);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
provider.setUserDetailsContextMapper(userDetailsContextMapper());
return provider;
}
@Bean
public UserDetailsContextMapper userDetailsContextMapper() {
UserDetailsContextMapper contextMapper = new MyUserDetailsContextMapper();
return contextMapper;
}
@Bean
public MyDatabaseAuthenticationProvider databaseAuthenticationProvider() {
return new MyDatabaseAuthenticationProvider();
}
除了一些用于映射和查找用户的自定义逻辑之外,“MyDatabaseAuthenticationProvider”或“MyUserDetailsContextMapper”类中实际上没有什么特别的。
应用程序不会崩溃,但显然不是我想向用户显示的页面。 :)
关于如何摆脱 StackOverflowError 有什么想法吗?
最佳答案
我遇到了同样的问题,这就是我的解决方案:
@Override
protected void configure(AuthenticationManagerBuilder
authManagerBuilder) throws Exception {
...
.userDetailsService(userDetailsService());
...
}
问题在于 userDetailsService 之后的括号 - 删除它们并且它按预期工作。 从您的代码片段中,我无法确定您从哪里获得 userDetailsService,对我来说,我有 @Autowired。
关于java - 所有提供者之后的 Spring Security java.lang.StackOverflowError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766106/