2010-03-07 68 views
1

我想通過編程方式驗證用戶登錄/傳遞使用春季安全,所以我需要訪問ProviderManager。我希望它被自動注入我的@Controller春季安全:autowire ProviderManager

我的代碼如下所示:

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] 

可能是什麼原因或者我怎麼能解決呢?

我在Spring 3.0.1中使用Spring Security 3.0.0-RC1,並且我沒有定義任何ProviderManager bean。我已成功使用:

@Resource 
private ProviderManager authenticationManager; 

在其他項目中,但在GAE中不支持javax.annotation.Resource

回答

9

有兩種AuthenticationManager S IN的背景:

  • org.springframework.security.authenticationManager是填充在<authentication-manager>
  • org.springframework.security.authentication.ProviderManager#0顯式聲明的身份驗證提供填充了隱式聲明的供應商(還記得我,匿名等)和代表作爲後備的org.springframework.security.authenticationManager身份驗證請求。

所以,我想你需要

@Autowired @Qualifier("org.springframework.security.authenticationManager") 
+0

謝謝,我不知道@Qualifier註釋。你知道它是否與「alias」屬性()似乎也解決了這個問題有關嗎? – 2010-03-07 23:13:15

+0

@Guido:是的,您的解決方案几乎以相同的方式工作。當您的解決方案選擇該bean時,「@ Qualifier」使用指定的名稱來選擇其中一個bean,因爲其別名與字段名稱相匹配。 – axtavt 2010-03-07 23:30:50

4

錯誤消息消失包括認證管理器的別名:

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

升級到Spring Security的3.0.0結局。