How to programmatically log user in with Spring Security 3.1

at. picture at. · Sep 30, 2011 · Viewed 14.2k times · Source

What's the proper way to programmatically log a web visitor in under a particular username in Spring and Spring Security 3.1? It seems the way I was doing it under 2.5 has changed a little. I'm sure there's a much better way of doing this now.

Basically, when I create a new user, I need to also log them in at the same time.

Answer

sourcedelica picture sourcedelica · Sep 30, 2011

Create an Authentication (usually a UsernamePasswordAuthenticationToken) and then call

SecurityContextHolder.getContext().setAuthentication(authentication)