2010-03-17 66 views
2

我嘗試在使用oAuth獲取憑據後使用用戶身份驗證(使用Twitter時,如果這有所影響)。據我所知,我可以直接將Authentication對象放到SecurityContextHolder中。下面是我該怎麼做:手動驗證用戶

Authentication auth = new TwitterOAuthAuthentication(member, 
userDetailsService.loadUserByUsername(member.getUsername()).getAuthorities()); 
SecurityContextHolder.getContext().setAuthentication(auth); 

這由於某種原因絕對沒有。我錯過了什麼,該怎麼做才能完成需要的工作?

回答

3

嘗試增加

auth.setAuthenticated(true); 

您將身份驗證上下文之前。

下面的代碼工作正常,我(過濾器類中):

final Authentication auth = new CustomAuthenticationToken(user); 
auth.setAuthenticated(true); 
SecurityContextHolder.getContext().setAuthentication(auth);