Announcement Announcement Module
No announcement yet.
How to reuse a persisted connection? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to reuse a persisted connection?

    I am working on integrating Spring Social with a GWT app I am building. The app is Twitter specific and the only way to login to the app is to authenticate via Twitter.

    The first time the user goes to the home page, as he is not logged in, he will be presented with a link to authenticate via Twitter. This link is generated in a process similar to the code block below:

    OAuthToken requestToken = createRequestToken();
    session.setAttribute(REQUEST_TOKEN_SESSION_KEY, requestToken);
    String loginLink = createLoginUrl(requestToken);
    The user clicks on the link, authorizes the app, and Twitter redirects back to the OAuth callback with a verifier. This time a block of code like the one given below is executed:

    private Connection<Twitter> createConnection(OAuthToken requestToken, String oauthVerifier) {
        TwitterConnectionFactory connectionFactory = (TwitterConnectionFactory) connectionFactoryLocator.getConnectionFactory(Twitter.class);
        OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations();
        AuthorizedRequestToken authorizedRequestToken = new AuthorizedRequestToken(requestToken, oauthVerifier);
        OAuthToken accessToken = oauthOperations.exchangeForAccessToken(authorizedRequestToken, null);
        return connectionFactory.createConnection(accessToken);
    Now let's pretend that I persist this connection for later use. As long as the user is not logged out and the session is not invalidated I can reuse it with no problem: I can store the ConnectionKey in the session and use it to fetch the connection from the database. But after the user logs out, the process above has to be repeated and after getting back the oauthVerifier, I see no way to avoid invoking connectionFactory.createConnection(accessToken) and instead reuse the persisted connection. Or am I missing something? Is there a way to reuse the persisted connection when the user wants to log in again?

    Thanks in advance,

  • #2
    How are you persisting this connection? If you're using JdbcUsersConnectionRepository/JdbcConnectionRepository, then you will be persisting it in the context of one of your application's users (identified by the user's local user ID) via ConnectionRepository.addConnection(Connection). Then when you want to get that connection back you can do so by calling one of the many methods on the ConnectionRepository. (See