StackOverflowError试图在Spring WebSecurityConfigurerAdapter中公开AuthenticationManager

Ian*_*las 3 java spring filter spring-security spring-java-config

我试图通过扩展WebSecurityConfigurerAdapter来创建一个Spring Security配置,基本上是这样的:

@EnableWebSecurity
@Configuration
public class StackOverflowSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.authenticationProvider(myUsernamePasswordProvider());
        auth.authenticationProvider(mySecurityTokenProvider());

        super.configure(auth);
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public MyPreAuthenticatedProcessingFilter myAuthenticationFilter() throws Exception {
        MyPreAuthenticatedProcessingFilter myAuthenticationFilter = new MyPreAuthenticatedProcessingFilter();
        myAuthenticationFilter.setAuthenticationManager(authenticationManager());

        return myAuthenticationFilter;
    }

}
Run Code Online (Sandbox Code Playgroud)

我看到了这个:

SEVERE: Servlet.service() for servlet [servlet] in context with path [/MyApp] threw exception [Filter execution threw an exception] with root cause
[INFO] [talledLocalContainer] java.lang.StackOverflowError
[INFO] [talledLocalContainer]   at org.springframework.security.authentication.AnonymousAuthenticationProvider.supports(AnonymousAuthenticationProvider.java:79)
[INFO] [talledLocalContainer]   at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:164)
[INFO] [talledLocalContainer]   at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:469)
[INFO] [talledLocalContainer]   at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199)
[INFO] [talledLocalContainer]   at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:469)
[INFO] [talledLocalContainer]   at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199)
[INFO] [talledLocalContainer]   at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:469)
[INFO] [talledLocalContainer]   at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199)
[INFO] [talledLocalContainer]   at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:469)
[INFO] [talledLocalContainer]   at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199)
[INFO] [talledLocalContainer]   at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:469)
[INFO] [talledLocalContainer]   at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199)
...
Run Code Online (Sandbox Code Playgroud)

我已经尝试更改我能想到的所有内容以正确获取AuthenticationManager并且没有得到StackOverflow错误而且我仍然卡住了.我发现的唯一一件事是这个缺陷,https://github.com/spring-projects/spring-security/issues/2732,在Spring Security中,当有"无效的配置尝试到在未配置身份验证时将AuthenticationManager公开为Bean".不幸的是,我不知道究竟是什么意思或如何解决这个问题.

这个Spring Security配置在Spring XML配置中工作,这是我尝试迁移到Spring Java Config.有没有更好的方法来配置我的Spring Security和/或将AuthenticationManager暴露给我的自定义身份验证过滤器?

Ian*_*las 12

我终于找到了问题.问题是我覆盖了错误的方法.我做了:

@Override
@Bean
public AuthenticationManager authenticationManager() throws Exception {
    return super.authenticationManagerBean();
}
Run Code Online (Sandbox Code Playgroud)

代替:

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}
Run Code Online (Sandbox Code Playgroud)

我最终压倒了一个类似但不正确的方法.该方法authenticationManager()用于对AuthenticationManager进行一些配置,authenticationManagerBean()用于将AuthenticationManager公开为可以自动装配和使用的Spring Bean.执行我所做的操作会导致必要的配置不会发生,而是以一种导致堆栈溢出的方式链接AuthenticationManagers.