Announcement Announcement Module
Collapse
No announcement yet.
programmatic login using spring security Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • programmatic login using spring security

    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?

  • #2
    The easiest way to log a user in is to set the Authentication on the SecurityContextHolder's context. For example:

    Code:
    Authentication authentication = new UsernamePasswordAuthenticationToken("username", "password",
                    AuthorityUtils.createAuthorityList("ROLE_USER"));
    SecurityContextHolder.getContext().setAuthentication(authentication);
    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.

    Comment

    Working...
    X