-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Circular reference when exposing a AuthenticationProvider with a dependency to HttpSecurity #16047
Comments
I think the circular reference caused by introducing |
That makes sense. If the initialization of the The other circular reference remains in PR #16050 , when there is only one Thank you very much. |
The construction of a That said, I'd like to understand your use case better. Can you please say why your provider needs an instance of |
Sure. To be fair, the method argument Lets say, I want to have a security configuration in a library with multiple authentication providers, which can be enabled or disabled and it would be also helpful when beans in the configuration process can be customized. The following snippet shows the configuration process to achieve this by splitting the configuration process into multiple protected methods and passing the @Configuration
@EnableWebSecurity
public class SecurityConfiguration {
// Application property
private boolean daoAuthenticationEnabled;
// Additional boolean flags to configure authentication
@Bean
public SecurityFilterChain securityFilterChain(@Nonnull HttpSecurity http) throws Exception {
// Split the configuration in multiple protected methods with a consistent method signature
configureAuthorization(http);
configureAnonymousAuthentication(http);
configuraDaoAuthentication(http);
configureFormLogin(http);
// Additional configuration methods
return http.build();
}
// A typical configuration method looks like this
protected void configureDaoAuthentication(@Nonnull HttpSecurity http) throws Exception {
if (daoAuthenticationEnabled) {
AuthenticationProvider daoAuthenticationProvider = daoAuthenticationProvider(http);
http.authenticationProvider(daoAuthenticationProvider);
}
}
// Some AuthenticationProvider's implement InitializingBean or just need dependency injection, so they are annotated with @Bean
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider(@Nonnull HttpSecurity http) throws Exception {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setHideUserNotFoundExceptions(true);
// Additional configuration but with no actual dependency to HttpSecurity at this point
return daoAuthenticationProvider;
}
} I'm not sure if the method call |
You can take a look at As for the dependency cycle, It feels like this ticket has moved into question territory at this point, so I'm going to close it and invite you to post to StackOverflow for any related discussions. You're welcome to post the SO link to this ticket so folks can continue to follow. Thanks again for reaching out! |
Hi,
there seems to be a strange behavior, when there is a Security Configuration which exposes
AuthenticationProvider
s as Spring@Bean
s, which useHttpSecurity
as a method argument. The application does not start due to circular references, depending on which Spring Security Version is in use:Spring Boot 3.2.7, Spring Security 6.2.5:
If there is only one
AuthenticationProvider
there is a circular reference:Exception:
If there is more than one
AuthenticationProvider
, everything is fine:Spring Boot 3.3.5, Spring Security 6.3.4:
In this setup, it doesn't matter if there is only one
AuthenticationProvider
bean or twoAuthenticationProvider
beans. The application does not start in both cases due to circular references.Analysis
I traced the cause to the
InitializeAuthenticationProviderManagerConfigurer
, which tries to default anAuthenticationProvider
in theAuthenticationManagerBuilder
, when there is exactly oneAuthenticationProvider
present. In version 6.2.5 the circular reference is triggered when the configurer tries to resolve the one Authentication bean. In version 6.3.4, the circular reference is triggered earlier.Expected behavior
Correct me if I'm wrong, i found similar issues regarding circular references, but I think this is something new. Is the method signature
AuthenticationProvider
as@Bean
with a dependency toHttpSecurity
a bad practise? Is it a bad idea to expose anAuthenticationProvider
at all? If the method argumentHttpSecurity
is removed, everything seems to work fine. The method argumentHttpSecurity
is primarily used to allow access to shared objects and to maintain a consistent method signature. I can provide a full example if necessary.The text was updated successfully, but these errors were encountered: