所有提供者之后的Spring Security java.lang.StackOverflowError异常

Bri*_*art 7 java spring spring-mvc spring-security

环境:

  • 春季4.1.6
  • Spring Security 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)
Run Code Online (Sandbox Code Playgroud)

这是我的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();
}
Run Code Online (Sandbox Code Playgroud)

除了用于映射和查找用户的一些自定义逻辑之外,"MyDatabaseAuthenticationProvider"或"MyUserDetailsContextMapper"类中没有什么特别之处.

该应用程序不会崩溃,但显然不是我想要向用户显示的页面.:)

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

zei*_*isi 9

我有同样的问题,这是我的解决方案:

@Override
protected void configure(AuthenticationManagerBuilder
authManagerBuilder) throws Exception {
...
.userDetailsService(userDetailsService());
...
}
Run Code Online (Sandbox Code Playgroud)

userDetailsS​​ervice之后括号的问题 - 删除它们并按预期工作.从你的代码片段我无法确定你从哪里获得userDetailsS​​ervice,对我来说,我已经@Autowired了.

  • 更清楚一点:**删除**行`.userDetailsS​​ervice(userDetailsS​​ervice())`,因为它是默认值,你创建了一个无限回归,将它传递给自己. (2认同)