nKognito
nKognito

Reputation: 6363

Manual authentication with spring security and remember me provider

I need to implement a manual login process. The authentication works fine:

UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword());
token.setDetails(new WebAuthenticationDetails(request));
Authentication authenticatedUser = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);

But how can I use remember me provider in this case? Thank you in advance

Upvotes: 4

Views: 2053

Answers (2)

romu31
romu31

Reputation: 827

Nice answer . it worked for me.

with new version of spring security :

RememberMeAuthenticationToken auth = new RememberMeAuthenticationToken("your key",userDetails,  autorities);
            String  p = request.getParameter("rememberme");
    //      your request need this parameter

            Authentication authenticatedUser = authenticationManager.authenticate(auth);
            SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
            myrememberMeService.loginSuccess(request, response, authenticatedUser);

Upvotes: 1

nKognito
nKognito

Reputation: 6363

The solution is the following:

  1. Check user's password
  2. Authorize user:

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword());
    token.setDetails(new WebAuthenticationDetails(request));                        
    Authentication authenticatedUser = authenticationManager.authenticate(token);
    SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    
  3. Autowire rememberMeService and call:

rememberMeServices.onLoginSuccess(request, response, authenticatedUser);

Upvotes: 4

Related Questions