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

标签 java spring spring-mvc spring-security

环境:

  • 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());
...
}

问题在于 userDetailsS​​ervice 之后的括号 - 删除它们并且它按预期工作。 从您的代码片段中,我无法确定您从哪里获得 userDetailsS​​ervice,对我来说,我有 @Autowired。

关于java - 所有提供者之后的 Spring Security java.lang.StackOverflowError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766106/

相关文章:

java - Worklight适配器它不是一个函数,它是 "object"。错误

java - Qt Jambi 与 JOGL 用于 java OpenGL 开发?

java - Spring JAXB2 解码重复的 xml 根元素名称

java - 我想启用矩阵变量,但它显示了消息

java - Android:如何在不将屏幕截图保存到 SD 卡的情况下在电子邮件中发送屏幕截图?

java - 如何在一个下拉列表中显示绑定(bind)在一起的 2 个元素

Java消息对象枚举Spring集成错误

java - 使用自定义用户通过数据库进行 Spring Security 身份验证

java - Spring - 使用 Web 服务时未加载属性文件中的值

java - 测试时如何删除路由的重复名称