#

Spring Acegi/Security auto-login

Usefull and thus noted:
In one of my current workprojects I had a registrationform the user filled. After formvalidation, I wanted to login the user automatically. This can be achieved in Spring / Acegi quite easy:

User user = createUser();
final RememberMeAuthenticationToken auth = new RememberMeAuthenticationToken(webKey, userAccount.getUser(), userAccount.getUser().getAuthorities()); 
auth.setAuthenticated(true); 
SecurityContextHolder.getContext().setAuthentication(auth);

Also logging user out (after database new creattion or session invalidation):

SecurityContextHolder.getContext().setAuthentication(null);
Tags:, , , ,

Leave a Reply »»

Note: All comments are manually approved to avoid spam. So if your comment doesn't appear immediately, that's ok. Have patience, it can take some days until I have the time to approve my comments.