Spring 安全性 java.lang.StackOverflowError 所有提供程序后异常

Spring Security java.lang.StackOverflowError exception after all providers

环境:

我有 2 个身份验证提供程序 - 一个访问 ActiveDirectory,然后一个访问我创建的自定义数据库提供程序。以在这些环境中的任何一个中的用户身份登录都可以完美地工作。用户通过身份验证,应用程序继续运行。

但是,当输入无效用户且两个提供商都无法进行身份验证时,我在页面上收到此异常:

java.lang.WhosebugError
    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" 类 中确实没有什么特别之处,除了一些用于映射和查找用户的自定义逻辑。

应用程序没有崩溃,但显然不是我要向用户显示的页面。 :)

关于如何摆脱 WhosebugError 的任何想法?

我遇到了同样的问题,这是我的解决方案:

@Override
protected void configure(AuthenticationManagerBuilder
authManagerBuilder) throws Exception {
...
.userDetailsService(userDetailsService());
...
}

问题是 userDetailsS​​ervice 后面的括号被删除了,它按预期工作。 从你的代码片段我不能确定你从哪里得到 userDetailsS​​ervice,对我来说我有它@Autowired。

我遇到了同样的问题,另一个解决方案适用于我的情况。不同的是,我只有用户名和密码,数据库身份验证。

@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
    authManagerBuilder
            .userDetailsService(userDetailsService())
            .passwordEncoder(passwordEncoder());
}

@Bean
UserDetailsService userDetailsService() {
    return new UserDetailsService();
}

修复方法是将 @Override 注释添加到 @Bean 注释方法:

@Bean
@Override
UserDetailsService userDetailsService() {
    return new UserDetailsService();
}