代码之家  ›  专栏  ›  技术社区  ›  Guido

弹簧安全:Autowire ProviderManager

  •  1
  • Guido  · 技术社区  · 14 年前

    我正在尝试使用Spring安全性对用户登录/通行证进行编程验证,因此我需要访问 ProviderManager . 我想把它自动注射到我的 @Controller .

    我的代码如下:

    import org.springframework.security.authentication.ProviderManager;
    
    // ...
    
    @Controller
    public class MyController {
    
        @Autowired
        private ProviderManager authenticationManager;
    

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

    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]
    

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

    我使用的是SpringSecurity3.0.0-RC1和Spring3.0.1,我没有定义任何 供应商经理 豆类。我已成功使用:

    @Resource
    private ProviderManager authenticationManager;
    

    在其他项目中,但是 javax.annotation.Resource 在GAE中不受支持。

    2 回复  |  直到 9 年前
        1
  •  9
  •   axtavt    14 年前

    有两个 AuthenticationManager 在上下文中:

    • org.springframework.security.authenticationManager 使用在中显式声明的身份验证提供程序填充 <authentication-manager>
    • org.springframework.security.authentication.ProviderManager#0 使用隐式声明的提供程序(记住我、匿名等)填充,并将身份验证请求委托给 org.springframework.security.authenticationmanager认证管理器 作为后退。

    所以,我想你需要

    @Autowired @Qualifier("org.springframework.security.authenticationManager")
    
        2
  •  4
  •   Guido    14 年前

    错误消息消失,包括身份验证管理器的别名:

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

    升级到Spring Security 3.0.0最终版。