简体   繁体   中英

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

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM