Set spring security remember me cookie after login

2020-03-30 05:27发布

问题:

I am building a mobile web app that has the option to login via facebook/twitter. I want the app to remember the login via Spring security's remember me functionality so that the user need to have to login frequently.

I have the parts that will call out to facebook and get the access_token that will identify the user. I can login the user using

    SecurityContextHolder.getContext().setAuthentication(
        new UsernamePasswordAuthenticationToken(principal, credentials, authorities));

I am trying to use similar approach to add remember me functionality by

    RememberMeAuthenticationToken auth = new RememberMeAuthenticationToken(key, principal, authorities);
    SecurityContextHolder.getContext().setAuthentication(auth);

While this logs in the user, it does not set the remember me cookie. What am I missing here?

Thanks!

回答1:

Try to inject the implementation of RememberMeServices interface and then try to do:

rememberMeServices.loginSuccess(request, response, auth);

Be sure that _spring_security_remember_me parameter is present in the request.