azure-sdk-for-java: [BUG] Circular references about restTemplateBuilder

Describe the bug A Spring Boot 2.7.4 application having dependencies on both spring-boot-starter-data-rest and spring-cloud-azure-starter-active-directory 4.4.0 fails to start due to circular references. Using spring-cloud-azure-starter-active-directory 4.3.0 works fine.

Exception or Stack Trace

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   com.azure.spring.cloud.autoconfigure.aad.AadAuthenticationFilterAutoConfiguration
┌─────┐
|  restTemplateBuilder defined in class path resource [org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.class]
↑     ↓
|  restTemplateBuilderConfigurer defined in class path resource [org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.class]
↑     ↓
|  messageConverters defined in class path resource [org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.class]
↑     ↓
|  jacksonHttpMessageConverter defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]
↑     ↓
|  linkCollector defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]
↑     ↓
|  selfLinkProvider defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]
↑     ↓
|  org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
↑     ↓
|  org.springframework.security.config.annotation.web.configuration.OAuth2ClientConfiguration$OAuth2ClientWebMvcSecurityConfiguration
↑     ↓
|  com.azure.spring.cloud.autoconfigure.aad.configuration.AadOAuth2ClientConfiguration
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

To Reproduce Create a new Spring Boot project at https://start.spring.io/ and select the following dependencies:

  • Rest Repositories
  • Azure Active Directory
  • Spring Security
  • OAuth2 Client image

Then enable AAD in the application.yaml file:

spring:
  cloud:
    azure:
      active-directory:
        enabled: true
        credential:
          client-id: whatever
          client-secret: whatever
        profile:
          tenant-id: whatever
        user-group:
          allowed-group-names: whatever

Code Snippet N/A

Expected behavior The Spring Boot application starts (and immediately terminates because it’s not a web application) without error.

Screenshots N/A

Setup (please complete the following information):

  • OS: Windows
  • IDE: IntelliJ
  • Library/Libraries:
  • org.springframework.boot:spring-boot-starter-data-rest:2.7.4
  • org.springframework.boot:spring-boot-starter-oauth2-client:2.7.4
  • org.springframework.boot:spring-boot-starter-security:2.7.4
  • com.azure.spring:spring-cloud-azure-starter-active-directory:4.4.0
  • Java version: 11
  • App Server/Environment: N/A
  • Frameworks: Spring Boot 2.7.4

Additional context This could be related to https://github.com/Azure/azure-sdk-for-java/pull/31048 Downgrading to com.azure.spring:spring-cloud-azure-starter-active-directory:4.3.0 solves the problem. Removing the org.springframework.boot:spring-boot-starter-data-rest dependency also “solves” the problem.

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Bug Description Added
  • Repro Steps Added
  • Setup information Added

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 2
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Just discovered this issue myself; following.

Hi, @mnhock

Thanks for reaching out. Actually, there is no updates in Spring Cloud Azure side. It’s caused by spring-data-rest, if you are not using spring-data-rest, you will not face this problem. We already created an issue in spring-data-rest side: https://github.com/spring-projects/spring-data-rest/issues/2193

Hi, @sigiswald, thanks for reaching out. I’ll investigate this issue and give feedback ASAP.

Downgrading to v4.3.0 was not an option for us since this was giving issues with the decryption of tokens of service principals. Instead, I created a simple workaround which you can easily embed in your project. The gist: https://gist.github.com/yoranvanoirschot/1b3c91df4f60b9d8522162fbee5ef4e3

Goodluck!

@saragluna could you please take a look at this issue, thanks.