I have a website that spring security for authentication.
It works fine.
When User registers the to site, I would like to login the user automatically to the website.
What is the best way to achieve this?
Printable View
I have a website that spring security for authentication.
It works fine.
When User registers the to site, I would like to login the user automatically to the website.
What is the best way to achieve this?
The easiest way to log a user in is to set the Authentication on the SecurityContextHolder's context. For example:
Note that in many instances Spring Security uses a User object as the principal instead of "username". However, depending on your usecase "username" will work just fine.Code:Authentication authentication = new UsernamePasswordAuthenticationToken("username", "password",
AuthorityUtils.createAuthorityList("ROLE_USER"));
SecurityContextHolder.getContext().setAuthentication(authentication);