2011-03-24 88 views
0

我想實現下面的春季自動登錄,但我的身份驗證管理器實例拋出下面的異常,而不是自動裝配。我如何從Spring手動獲取它的實例?我沒有使用彈簧控制器,我使用JSF請求作用域bean。在容器嘗試自動裝載authenticationManager時,我在運行時遇到以下異常。 requestCache正常。我應該在UserDetailsS​​ervice實現(userManager)上使用方法嗎?我沒有看到由UserDetailsS​​ervice公開的採用UsernamePasswordAuthenticationToken objet的適當方法。有任何想法嗎? 配置:春季自動登錄問題

<http access-denied-page="/auth/denied.html"> 
     <intercept-url 
      pattern="/**/*.xhtml" 
      access="ROLE_NONE_GETS_ACCESS" /> 
     <intercept-url 
      pattern="/auth/**" 
      access="ROLE_ANONYMOUS,ROLE_USER" /> 
     <intercept-url 
      pattern="/auth/*" 
      access="ROLE_ANONYMOUS" /> 
     <intercept-url 
      pattern="/registered/*" 
      access="ROLE_USER" /> 
      <intercept-url 
      pattern="/*" 
      access="ROLE_ANONYMOUS" /> 
     <form-login 
      login-processing-url="/j_spring_security_check.html" 
      login-page="/auth/login.html" 
      default-target-url="/registered/home.html" 
      authentication-failure-url="/auth/login.html" /> 
     <logout invalidate-session="true" 
       logout-success-url="/" 
       logout-url="/auth/logout.html"/> 
     <anonymous username="guest" granted-authority="ROLE_ANONYMOUS"/> 
     <remember-me user-service-ref="userManager" key="e37f4b31-0c45-11dd-bd0b-0800200c9a66"/> 
    </http> 
    <!-- Configure the authentication provider --> 
    <authentication-manager alias="am"> 
     <authentication-provider user-service-ref="userManager"> 
       <password-encoder ref="passwordEncoder" /> 
     </authentication-provider> 
    </authentication-manager> 

異常 注射自動裝配依賴的失敗;嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝入字段:protected org.springframework.security.authentication.AuthenticationManager com.dc.web.actions.SignUpDetail.authenticationManager;嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有定義[org.springframework.security.authentication.AuthenticationManager]類型的唯一bean:期望單個匹配的bean,但找到2:[org.springframework.security.authentication.ProviderManager #0,org.springframework.security.authenticationManager] javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)

@Named 

@Scope( 「請求」) 公共類註冊 {

@Inject 
RequestCache requestCache; 

@Inject 
protected AuthenticationManager authenticationManager; 

public String login(){ 
authenticateUserAndSetSession(utilities.getLoggedInUser(), (HttpServletRequest)  FacesUtils.getExternalContext().getRequest()); 
    return "/home.html"; 
} 
private void authenticateUserAndSetSession(Users user, 
      HttpServletRequest request) 
     { 
    UserDetails details = userManager.loadUserByUsername(user.getUsername()); 
    UsernamePasswordAuthenticationToken usernameAndPassword = 
     new UsernamePasswordAuthenticationToken(
      user.getUsername(), "pwd", details.getAuthorities()); 

    // Authenticate, just to be sure 
    Authentication auth = authenticationManager.authenticate(usernameAndPassword); 

    // Place the new Authentication object in the security context. 
    SecurityContextHolder.getContext().setAuthentication(auth); 
} 
+1

根據例外情況,您有兩個身份驗證管理器。你需要告訴Autowire你想使用哪一個 – Anon 2011-03-24 11:40:50

回答

1

這是由Sp ring Security內部聲明第二個AuthenticationManager,以便您擁有其中兩個。你需要選擇其中之一,alias

<authentication-manager alias = "am"> 
    ... 
</authentication-manager> 

@Inject @Named("am") 
protected AuthenticationManager authenticationManager; 
+0

感謝您的幫助。我想我只需要在用戶自動登錄時將角色關聯到用戶,但我不確定如何執行此操作。當我在SecurityContextHolder.getContext()。setAuthentication(authenticatedUser)之後向/registered/home.html發出請求時,我仍然被重定向到我的登錄頁面(添加了自動登錄代碼後)。呼叫。我在上面添加了我的攔截網址。有任何想法嗎? – c12 2011-03-24 12:18:40

+0

@ c12:身份驗證提供程序應將角色關聯到身份驗證令牌本身,因此我不知道爲什麼它不起作用。 – axtavt 2011-03-24 12:27:14

+0

是的,它的奇怪,認證對象說im認證,並且我的校長是正確的 – c12 2011-03-24 12:30:46

1

@ C12如果使用的是定製的基於角色的權限有可能是內部當局收集您的的UserDetails是不填充在loadUserByUsername你的方法UserManager class;如果是這種情況,您需要手動將特定角色關聯到權限集合。請查看下面的代碼片段(理想情況下,它應該是loadUserByUsername方法體的一部分);假設...... 一)MYUSER是你自己的用戶對象 B)MyUserRole是你自己的角色對象 C)爲userDetails是您將從loadUserByUsername方法

MyUser myUser = userDao.getUserByUsername(username); 
Set<GrantedAuthority> grantedAuthSet = new HashSet<GrantedAuthority>(); 
Set<MyUserRole> myUserRoleSet = myUser.getRoleSet(); 

for (Iterator<MyUserRole> iterator = myUserRoleSet.iterator(); iterator.hasNext();) { 
    MyUserRole userRole = (MyUserRole) iterator.next(); 
    grantedAuthSet.add(new SimpleGrantedAuthority(userRole.getName())); 
} 

List<GrantedAuthority> grantedAuthList = new ArrayList<GrantedAuthority>(grantedAuthSet); 
myUser.setAuthorities(grantedAuthList); 
UserDetails userDetails = new User(myUser.getUsername(), myUser.getPassword(), true, true, true, true, myUser.getAuthorities()); 

希望這有助於返回的內容!