Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to programmatically authenticate with Spring Security and make it persistent?

I'm using the following method in a Spring Controller to allow authentication via Ajax. It works, but it doesn't seem to create a cookie or anything that makes the authentication persistent.

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public LoginStatus login(@RequestParam("j_username") String username,
                         @RequestParam("j_password") String password) {

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);

    try {
        Authentication auth = authenticationManager.authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(auth);
        return new LoginStatus(auth.isAuthenticated(), auth.getName());
    } catch (BadCredentialsException e) {
        return new LoginStatus(false, null);
    }
}

What do I need to do to make the authentication persistent?

like image 913
Matt Raible Avatar asked Feb 23 '11 04:02

Matt Raible


1 Answers

Make sure

  • you have SecurityContextPersistenceFilter configured
  • you are not setting create-session attribute of <security:http> tag to none.
like image 137
Boris Kirzner Avatar answered Oct 31 '22 18:10

Boris Kirzner