views:

722

answers:

2

I am trying to programatically validate a user login/pass using Spring Security, so I need to have access to the ProviderManager. I would like it to be automatically injected into my @Controller.

My code looks like:

import org.springframework.security.authentication.ProviderManager;
...

@Controller
public class MyController {

    @Autowired
    private ProviderManager authenticationManager;

But when I try to run the application I get this error message:

No unique bean of type [org.springframework.security.authentication.ProviderManager] is defined: 
expected single matching bean but found 2: 
[org.springframework.security.authentication.ProviderManager#0, org.springframework.security.authenticationManager]

What could be the cause or how could I solve it?

I am using Spring Security 3.0.0-RC1 with Spring 3.0.1, and I've not defined any ProviderManager bean. I've succesfully used:

@Resource
private ProviderManager authenticationManager;

in other projects, but javax.annotation.Resource it is not supported in GAE.

+3  A: 

There are two AuthenticationManagers in the context:

  • org.springframework.security.authenticationManager is populated with authentication providers explicitly declared in <authentication-manager>
  • org.springframework.security.authentication.ProviderManager#0 is populated with implicitly declared providers (remember me, anonymous and so on) and delegates the authentication request to org.springframework.security.authenticationManager as a fallback.

So, I guess you need

@Autowired @Qualifier("org.springframework.security.authenticationManager")
axtavt
Thank you, I didn't know @Qualifier annotation. Do you know if it is related someway with the "alias" attribute (<sec:authentication-manager alias="authenticationManager">) that seems to solve the problem as well ?
Guido
@Guido: Yes, your solution works almost the same way. `@Qualifier` uses the specified name to choose one of the beans, when your solution chooses that bean because its alias matches the field name.
axtavt
+1  A: 

The error message goes away including an alias for the authentication-manager:

<sec:authentication-manager alias="authenticationManager">

and upgrading to Spring Security 3.0.0 finale.

Guido