spring-projects / spring-security

Spring Security
http://spring.io/projects/spring-security
Apache License 2.0
8.73k stars 5.86k forks source link

Cannot Autowire ClientRegistrationRepository for a Servlet (works for Webflux) #15493

Closed dreamstar-enterprises closed 1 month ago

dreamstar-enterprises commented 1 month ago

For some reason, when I do this

@Configuration
@EnableWebSecurity
internal class DefaultSecurityConfig () {

    @Autowired
    private lateinit var clientRegistrationRepository: ClientRegistrationRepository

I keep getting this:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'persistenceExceptionTranslationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'persistenceExceptionTranslationPostProcessor' parameter 0: Invalid bean definition with name 'clientRegistrationRepository' defined in class path resource [com/example/authorizationserver/auth/security/repositories/ClientRegistrationRepository.class]: factory-bean reference points back to the same bean definition
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:801) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:545) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332) ~[spring-beans-6.0.13.jar:6.0.13]

But when I keep the bean in the same main config class, the error goes away. It's NOT an issue with the Webflux server

So, I'm guessing this is a bug?

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior.

Expected behavior A clear and concise description of what you expected to happen.

Sample

A link to a GitHub repository with a minimal, reproducible sample.

Reports that include a sample will take priority over reports that do not. At times, we may require a sample, so it is good to try and include a sample up front.

jzheaux commented 1 month ago

Thanks for reaching out, and I'm sorry that you are having difficulty, @dreamstar-enterprises.

I think this part of the error message offers a hint:

Invalid bean definition with name 'clientRegistrationRepository' defined in class path resource [com/example/authorizationserver/auth/security/repositories/ClientRegistrationRepository.class]: factory-bean reference points back to the same bean definition

It indicates that it's complaining about a bean of yours (which also happens to be named ClientRegistrationRepository) and not a Security bean. If I were to guess, your ClientRegistrationRepository is a Spring Data bean, so the complaint is about Spring Data not being able to post-process that repository.

First, I'd try ensuring that your bean is of a different name from the one produced by Spring Security.

If that doesn't work, I'd recommend that you post this question to StackOverflow with a tag of spring-data so that the Spring Data maintainers and community see it and can help you get to the bottom of it. You also may find https://github.com/spring-projects/spring-framework/issues/20330 helpful.

dreamstar-enterprises commented 1 month ago

Thanks Josh, another helpful post - so thank you. I think this may be because in the reactive version, I put 'reactive' infront of the bean names, and in the servlet, I may have created conflicting beans with the same name. Will try to tackle this again, later today, and feedback on progress.

dreamstar-enterprises commented 1 month ago

It worked! I changed the bean name, and also was careful with the class name that contained it (making sure the names were not the same). Thank you.