Home > Mobile >  I get stackOverflowError in spring security test
I get stackOverflowError in spring security test

Time:10-03

Security configuration class

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConf {

    @Bean
    public AuthenticationManager authenticationManagerBean(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

}

Controller

@RestController
public class Controller {
    
    @GetMapping("/test")
    public String test(){
        return "test";
    }
}

Test class

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = Controller.class)
@ComponentScan("com.example.demo")
public class DemoApplicationTests {

    @Test
    public void test() {
    }
}

I have a simple application, if I try to start the main it works, however when I start my test class I get this error:

java.lang.StackOverflowError
            at java.base/java.lang.StackTraceElement.of(StackTraceElement.java:526)
            at java.base/java.lang.Throwable.getOurStackTrace(Throwable.java:828)
            at java.base/java.lang.Throwable.getStackTrace(Throwable.java:820)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:79)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
            at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
    and it always continues the same way ...

in debug I tried to recover the error but I only see this java.lang.IllegalStateException: Failed to unwrap proxied object.

The test only works if I remove the authenticationManagerBeanbut i need authenticationManagerBean to inject AutthenticationManager in my app.

I know that I could use WebSecurityConfigurerAdapter and then add the bean going to override the metood of this class. But as WebSecurityConfigurerAdapter is deprecated, I would not want to use it.

this problem arose from the fact that I have switched from WebSecurityConfigurerAdapter to SecurityFilterChain and am getting this error as I changed the config class

I created this simple project which replicate the error of my main project. If you try to start the application it works, but the test doesn't. https://github.com/lako12/demo

The project is very simple, there are only 3 classes plus a test class.

CodePudding user response:

Do you actually need the AuthenticationManager in your application? From what I understand, you want to configure a SecurityFilterChain so you could just create this Bean instead:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConf {

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
      http.authorizeRequests().anyRequest().authenticated(); //or whatever else you need
      return http.build();
  }
}

CodePudding user response:

I opened an issue on spring github and it was reported as a bug that will be fixed by spring framework version 5.3.24. This is the link of the discussion. https://github.com/spring-projects/spring-framework/issues/29215

  • Related