Results 1 to 2 of 2

Thread: programmatic login using spring security

  1. #1

    Question 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. #2
    Join Date
    Jan 2008
    Posts
    1,833

    Default

    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.
    Rob Winch
    Twitter @rob_winch
    Spring Security Lead
    Spring by Pivotal

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •