After upgrading org.springframework.boot from 2.5.6 to 2.6.2 version the following error appears on application startup:
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| securityConfiguration
↑ ↓
| org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
└─────┘
I know from other questions that the easy workaround is to use the option allow-cirular-references, but I would like to really solve the problem rather than use a workaround.
This is my SecurityConfiguration class:
package com.mycompany.myapp.servicex.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("${application.myapp.in-browser-allowed-origins}")
private String[] inBrowserAllowedOrigins;
private final String[] inBrowserAllowedMethods = new String[]{"POST", "OPTIONS"};
@Override
protected void configure(final HttpSecurity httpSecurity) throws Exception {
httpSecurity.cors()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.csrf().disable()
.formLogin().disable()
.httpBasic().disable()
.logout().disable();
}
@Bean
public WebMvcConfigurer corsConfigurer()
{
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/in-browser/login")
.allowedOrigins(inBrowserAllowedOrigins)
.allowedMethods(inBrowserAllowedMethods);
registry.addMapping("/**").allowedOrigins();
}
};
}
}
Does anybody know how can I programmatically break the cyclic dependency?
I was already playing around with the @Lazy annotation on the corsConfigurer bean and in the inBrowserAllowedOrigins attribute with no success.