Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Spring Social Remember me with Facebook/Twitter login

  1. #1
    Join Date
    Aug 2011
    Location
    California
    Posts
    6

    Default Spring Social Remember me with Facebook/Twitter login

    Is it possible to have a remember me functionality with Facebook and Twitter login using Spring social?

    I'm using Spring Social 1.0.0.RC2 and Spring Security 3.0.5.RELEASE.

    Thanks

  2. #2
    Join Date
    Aug 2011
    Location
    California
    Posts
    6

    Default

    I solved this by making my own signin controller and calling the loginSuccess method of the TokenBasedRememberMeServices class.

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Cool. I wonder, could you reuse the existing ProviderSignInController and plug in a custom SignInAdapter that did this additional RememberMe work? Just wondering if that would also work for you.
    Keith Donald
    Core Spring Development Team

  4. #4
    Join Date
    Aug 2011
    Location
    California
    Posts
    6

    Default

    Quote Originally Posted by Keith Donald View Post
    Cool. I wonder, could you reuse the existing ProviderSignInController and plug in a custom SignInAdapter that did this additional RememberMe work? Just wondering if that would also work for you.
    I thought about this method but it appeared it would not work because the SignInAdapter interface has the following signature for the signIn method: String signIn(String userId, Connection<?> connection, NativeWebRequest request);

    onLoginSuccess of TokenBasedRememberMeServices requires a HttpServletRequest request and HttpServletResponse response and I'm not sure how those fields can be passed down to the adapter method.
    Signature of onLoginSuccess: public void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication)

    If I missed something obvious please let me know.

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    NativeWebRequest provides access to the Native HttpServletRequest and HttpServletResponse. Check the API JavaDocs for details. Quickstart I believe also shows this.

    Let me know if it in fact works out for you b/c our SignInController should be flexible enough to support cases like this.

    Keith
    Keith Donald
    Core Spring Development Team

  6. #6
    Join Date
    Aug 2011
    Location
    California
    Posts
    6

    Default

    Quote Originally Posted by Keith Donald View Post
    NativeWebRequest provides access to the Native HttpServletRequest and HttpServletResponse. Check the API JavaDocs for details. Quickstart I believe also shows this.

    Let me know if it in fact works out for you b/c our SignInController should be flexible enough to support cases like this.

    Keith
    Thanks, I completely overlooked this. I'm going to test it tomorrow and let you know the results, but it should work.

  7. #7
    Join Date
    Aug 2011
    Location
    California
    Posts
    6

    Default

    Thanks, it worked. I was over thinking things.

  8. #8
    Join Date
    Jul 2008
    Posts
    116

    Default

    Thanks guys. I just ran into this myself. For anyone else wondering:

    This assumes you have set the "alwaysRemember" flag to true otherwise you would somehow need to pass the parameter value (e.g. _spring_security_remember_me) as well. I couldn't figure out how to do this nicely. Implement your own version of SignInAdapter and here's the signIn method.

    Code:
    public String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) {
            User user = userService.findUser(new ObjectId(localUserId), true);
    
            // set user in secure context
            Principal principal = new Principal(user);
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), principal.getAuthorities()));
    
            // add remember me
            rememberMeServices.loginSuccess(request.getNativeRequest(HttpServletRequest.class), request.getNativeResponse(HttpServletResponse.class), SecurityContextHolder.getContext().getAuthentication());
    
            return extractOriginalUrl(request);
        }
    Last edited by bjornharvold; Sep 26th, 2011 at 03:41 PM.

  9. #9
    Join Date
    Jan 2006
    Location
    San Francisco
    Posts
    10

    Default

    Hey Bjorn,

    Thanks for the post. Was looking at doing this exact thing myself. A couple of quick questions:

    Are you still adding the spring_security_remember_me parameter to the request?, or does the rememberMeService take care of this?

    Are you redirecting(client) after extracting the original URL?

    I tried this out, and it does not seem like the REMEMBER_ME cookie is being set....I am probably doing something stupid here.

    Thanks,

    Joe

  10. #10
    Join Date
    Jul 2008
    Posts
    116

    Default

    Hi Joe,

    You have to set alwaysRemember to true for this to work. Have not come up with a way to elegantly passing the spring remember me param here.

    Here's the rest of the code:
    Code:
    private String extractOriginalUrl(NativeWebRequest request) {
            HttpServletRequest nativeReq = request.getNativeRequest(HttpServletRequest.class);
            HttpServletResponse nativeRes = request.getNativeResponse(HttpServletResponse.class);
            SavedRequest saved = requestCache.getRequest(nativeReq, nativeRes);
            if (saved == null) {
                return null;
            }
            requestCache.removeRequest(nativeReq, nativeRes);
            removeAutheticationAttributes(nativeReq.getSession(false));
            return saved.getRedirectUrl();
        }
    
        private void removeAutheticationAttributes(HttpSession session) {
            if (session == null) {
                return;
            }
            session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
        }

Posting Permissions

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