Spring Security:autowire ProviderManager

Gui*_*ido 1 google-app-engine spring spring-security

我正在尝试使用Spring Security以编程方式验证用户登录/传递,因此我需要访问ProviderManager.我希望它能自动注入我的@Controller.

我的代码看起来像:

import org.springframework.security.authentication.ProviderManager;

// ...

@Controller
public class MyController {

    @Autowired
    private ProviderManager authenticationManager;
Run Code Online (Sandbox Code Playgroud)

但是当我尝试运行应用程序时,我收到以下错误消息:

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]
Run Code Online (Sandbox Code Playgroud)

可能是什么原因或我如何解决?

我使用Spring Security 3.0.0-RC1和Spring 3.0.1,我没有定义任何ProviderManagerbean.我成功使用了:

@Resource
private ProviderManager authenticationManager;
Run Code Online (Sandbox Code Playgroud)

在其他项目中,但javax.annotation.ResourceGAE不支持.

axt*_*avt 9

AuthenticationManager上下文中有两个:

  • org.springframework.security.authenticationManager 填充了显式声明的身份验证提供程序 <authentication-manager>
  • org.springframework.security.authentication.ProviderManager#0填充了隐式声明的提供程序(记住我,匿名等)并将身份验证请求委派org.springframework.security.authenticationManager为回退.

所以,我想你需要

@Autowired @Qualifier("org.springframework.security.authenticationManager")
Run Code Online (Sandbox Code Playgroud)