Skip to content
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

AuthenticationConfiguration.lazyBean(AuthenticationManager.class) should honor @Primary if multiple candidates are available #3912

Closed
cheppsn opened this issue Jun 1, 2016 · 3 comments
Assignees
Labels
in: config An issue in spring-security-config type: enhancement A general enhancement
Milestone

Comments

@cheppsn
Copy link

cheppsn commented Jun 1, 2016

Using Spring Boot 1.3.4, Spring Security 4.0.4:

I encounter the following Exception for some (MockMvc) Tests that I want to run:

Caused by: java.lang.IllegalArgumentException: Expecting to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager, but found [docAuthenticationManager, webAuthenticationManager, globalAuthenticationManager]
    at org.springframework.util.Assert.isTrue(Assert.java:68)
    at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration.lazyBean(AuthenticationConfiguration.java:116)
    at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration.getAuthenticationMangerBean(AuthenticationConfiguration.java:128)
    at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration.getAuthenticationManager(AuthenticationConfiguration.java:84)
    at org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration.authenticationManager(GlobalMethodSecurityConfiguration.java:257)
    at org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration.methodSecurityInterceptor(GlobalMethodSecurityConfiguration.java:123)
    at ch.belimo.cloud.server.services.security.SecurityConfiguration$MethodSecurityConfiguration$$EnhancerBySpringCGLIB$$8864b9ed.CGLIB$methodSecurityInterceptor$8(<generated>)
    at ch.belimo.cloud.server.services.security.SecurityConfiguration$MethodSecurityConfiguration$$EnhancerBySpringCGLIB$$8864b9ed$$FastClassBySpringCGLIB$$b67a6873.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
    at com.mystuff.SecurityConfiguration$MethodSecurityConfiguration$$EnhancerBySpringCGLIB$$8864b9ed.methodSecurityInterceptor(<generated
...

I tried to fix the issue by defining an @Primary annotated AuthenticationManager instance for my Test, but this has no effect. The reason being that the AuthenticationConfiguration.lazyBean() Method does not honor @Primary, if multiple candidates are found:

String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
            applicationContext, interfaceName);
    if (beanNamesForType.length == 0) {
        return null;
    }
    Assert.isTrue(beanNamesForType.length == 1,
            "Expecting to only find a single bean for type " + interfaceName
                    + ", but found " + Arrays.asList(beanNamesForType));
@rwinch rwinch added this to the 4.2.0 M1 milestone Jun 14, 2016
@rwinch rwinch added in: config An issue in spring-security-config type: enhancement A general enhancement status: ideal-for-contribution An issue that we actively are looking for someone to help us with labels Jun 14, 2016
@rwinch
Copy link
Member

rwinch commented Jun 14, 2016

Thanks for the report @cheppsn! I have scheduled this for 4.2 M1

@rwinch rwinch removed this from the 4.2.0 M1 milestone Sep 23, 2016
@rwinch
Copy link
Member

rwinch commented Sep 23, 2016

Dropping from 4.2 as we are keeping this release focused on community contributions so the team can get Spring Security 5.0 out

shazin added a commit to shazin/spring-security that referenced this issue Nov 21, 2016
shazin added a commit to shazin/spring-security that referenced this issue Dec 5, 2016
shazin added a commit to shazin/spring-security that referenced this issue Dec 5, 2016
shazin added a commit to shazin/spring-security that referenced this issue Dec 5, 2016
@kagof
Copy link
Contributor

kagof commented Nov 1, 2018

I've created PR #6035 to resolve this. I know that @shazin created #4136 as well, however it is now nearly two years old and full of conflicts.

@rwinch rwinch added this to the 5.2.0.M1 milestone Nov 14, 2018
@rwinch rwinch removed the status: ideal-for-contribution An issue that we actively are looking for someone to help us with label Nov 14, 2018
@rwinch rwinch self-assigned this Nov 14, 2018
jer051 pushed a commit to jer051/spring-security that referenced this issue Nov 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: config An issue in spring-security-config type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants