2014-03-06 174 views
0

我想在註冊後自動登錄用戶。以下是自動登錄春季安全:自動登錄問題

private boolean autoLogin(HttpServletRequest request, User user) { 

    SimpleGrantedAuthority auth = new SimpleGrantedAuthority("ADMIN"); 
    Collection<SimpleGrantedAuthority> authorities = new HashSet<SimpleGrantedAuthority>(); 
    authorities.add(auth); 

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
      user.getEmail(), user.getPassword(), authorities); 

    token.setDetails(new WebAuthenticationDetails(request)); 
    authenticationManager.authenticate(token); 
    SecurityContextHolder.getContext().setAuthentication(token); 

    return true; 
} 

和該檢查登錄的用戶代碼的攔截器內部碼

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 

問題是當我調試代碼(自動登錄後)主要目的已登錄用戶的電子郵件地址而不是UserDetails對象。

事情工作正常,當我登錄使用彈簧安全登錄表單。

+0

http://stackoverflow.com/questions/4664893/how-to-manually-set-an-authenticated-user-in-spring-security-springmvc?rq=1我在這裏找到了我的答案 – Shahzeb

回答

0

您缺少重新分配AuthenticationManager.authenticate()的回報。

這條線:

authenticationManager.authenticate(token); 

應該是:

token = authenticationManager.authenticate(token); 

這應該解決的事情。