0

i am trying to auto login user after signup. Here is code for auto login

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;
}

and inside an interceptor that check logged in user code is

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

Problem is when i debug the code (after auto login) the principal object has logged in user's email address instead of UserDetails object.

Things working fine when i log in useing spring security login form.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79
  • http://stackoverflow.com/questions/4664893/how-to-manually-set-an-authenticated-user-in-spring-security-springmvc?rq=1 i found my answer here – Shahzeb Khan Mar 06 '14 at 11:36

1 Answers1

0

You're missing re-assigning the return from AuthenticationManager.authenticate().

This line:

authenticationManager.authenticate(token);

should be:

token = authenticationManager.authenticate(token);

That should fix things.

Rob Lockwood-Blake
  • 4,688
  • 24
  • 22