Home > Net >  How to migrate from WebSecurityConfigurerAdapter to SecurityFilterChain when using Azure Active Dire
How to migrate from WebSecurityConfigurerAdapter to SecurityFilterChain when using Azure Active Dire

Time:11-26

WebSecurityConfigurerAdapter is deprecated and I am trying to migrate to SecurityFilterChain. The code change needed was really very little. But the problem is that I am using azure active directory in my spring boot project:

<dependency>
  <groupId>com.azure.spring</groupId>
  <artifactId>spring-cloud-azure-starter-active-directory</artifactId>      
</dependency>
<dependency>
  <groupId>com.azure.spring</groupId>
  <artifactId>spring-cloud-azure-dependencies</artifactId>
  <version>4.4.1</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>

Azure dependencies seem to be still using WebSecurityConfigurerAdapter in the following classes:

AadResourceServerWebSecurityConfigurerAdapter
AadWebSecurityConfigurerAdapter
AadResourceServerConfiguration
AadWebApplicationConfiguration

As a result I am getting the following error when starting the application:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource .....

and down in the stack trace:

Caused by: java.lang.IllegalStateException: Found WebSecurityConfigurerAdapter as well as SecurityFilterChain. Please select just one.

There seems to be no update for Azure Active directory dependency yet. Is there a way to use SecurityFilterChain when using Azure AD?

CodePudding user response:

I found the solution. I had to exclude the AadAutoConfiguration class in my main application class:

@SpringBootApplication(exclude = AadAutoConfiguration.class)
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

and then define a custom one:

@Configuration(proxyBeanMethods = false)
@Import({AadPropertiesConfiguration.class})
public class AadAutoConfiguration {

  @Bean
  @ConditionalOnMissingBean(JwtDecoder.class)
  public JwtDecoder jwtDecoder(
      RestTemplateBuilder restTemplateBuilder,
      AadAuthenticationProperties aadAuthenticationProperties) {
    AadResourceServerConfiguration configuration =
        new AadResourceServerConfiguration(restTemplateBuilder);
    return configuration.jwtDecoder(aadAuthenticationProperties);
  }
}
  • Related