环境:
我有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。