Skip to content

Spring Circular Dependency with HttpSecurityConfiguration #12979

Closed
@santinozanone

Description

@santinozanone

Summary

i am having a problem with a circular dependency error. it is thrown when i define a UserDetailsService bean in my securityConfig class, however if i define that same bean in my authenticationConfig class,the error is gone and i dont get why.

Actual Behavior

this is the log:
Exception: org.springframework.beans.factory.UnsatisfiedDependencyException.
Message: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'initialAuthenticationFilter':
Error creating bean with name 'initialAuthenticationFilter': Unsatisfied dependency expressed through field 'manager':
Error creating bean with name 'authManager' defined in class path resource [root/AuthenticationConfig.class]:
Unsatisfied dependency expressed through method 'authManager' parameter 0:
Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.HttpSecurityConfiguration.httpSecurity'
defined in class path resource [org/springframework/security/config/annotation/web/configuration/HttpSecurityConfiguration.class]:
Failed to instantiate [org.springframework.security.config.annotation.web.builders.HttpSecurity]:
Factory method 'httpSecurity' threw exception with message: Error creating bean with name 'securityConfig':
Requested bean is currently in creation: Is there an unresolvable circular reference?
and then this diagram:

The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| securityConfig (field private root.filters.InitialAuthenticationFilter root.SecurityConfig.initialAuthenticationFilter)
↑ ↓
| initialAuthenticationFilter (field private org.springframework.security.authentication.AuthenticationManager root.filters.InitialAuthenticationFilter.manager)
↑ ↓
| authManager defined in class path resource [root/AuthenticationConfig.class]
↑ ↓
| org.springframework.security.config.annotation.web.configuration.HttpSecurityConfiguration.httpSecurity defined in class path resource [org/springframework/security/config/annotation/web/configuration/HttpSecurityConfiguration.class]
└─────┘

Expected Behavior

If i move the bean definition to any other class, it works, but when defined in securityConfig it doesnt, and i cannot find the circular dependency that spring is telling me about

Configuration

Version

Spring boot version 3.0.5
Spring security core 6.0.2

Sample

https://github.com/santinozanone/SpringHttpSecurityCircularDependency.git

Metadata

Metadata

Assignees

Labels

in: configAn issue in spring-security-configstatus: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions