<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>My software development blog.

  var _gaq = _gaq || [];
  _gaq.push([‘_setAccount’, ‘UA-18081753-5’]);
  _gaq.push([‘_trackPageview’]);

  (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
    var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
  })();</description><title>Harmonic Development</title><generator>Tumblr (3.0; @harmonicdevelopment)</generator><link>http://harmonicdevelopment.tumblr.com/</link><item><title>Spring Security and Spring Social Demo</title><description>&lt;a href="https://github.com/sdouglass/spring-security-social"&gt;Spring Security and Spring Social Demo&lt;/a&gt;: &lt;p&gt;I have received several requests for source code to go along with my previous posts about integrating Spring Social into a Spring MVC and Spring Security Web app. I’ve now posted a demo Web application to GitHub, which should be relatively easy to download and run.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://github.com/sdouglass/spring-security-social"&gt;http://github.com/sdouglass/spring-security-social&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Hopefully this will be useful for folks. It has already been useful for me. While writing this I discovered some issues with how I had integrated Spring Social into my own applications previously. I’m either going to edit my previous posts, or perhaps make new followup posts, to cover what I am doing differently now.&lt;/p&gt;
&lt;p&gt;To run the demo app, you will need to create your own applications on Twitter and Facebook:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://dev.twitter.com/apps/new"&gt;https://dev.twitter.com/apps/new&lt;/a&gt;&lt;br/&gt;&lt;a href="https://developers.facebook.com/apps"&gt;https://developers.facebook.com/apps&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You will need to create a file src/main/resources/spring-security-social.properties and set the following properties:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;site.url - URL used in the OAuth process&lt;/li&gt;
&lt;li&gt;social.crypto.password - password for encrypting/decrypting store OAuth secrets&lt;/li&gt;
&lt;li&gt;twitter.app.consumerKey - the consumer key for your Twitter application&lt;/li&gt;
&lt;li&gt;twitter.app.consumerSecret - the consumer secret for your Twitter application&lt;/li&gt;
&lt;li&gt;facebook.app.clientId - the client id for your Facebook application&lt;/li&gt;
&lt;li&gt;facebook.app.clientSecret - the client secret for your Facebook application&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;With all that in place you should be able to run the app with “mvn jetty:run”.&lt;/p&gt;
&lt;p&gt;This app supports the following features:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;signing up by creating an account with a username and password&lt;/li&gt;
&lt;li&gt;signing in with a username and password&lt;/li&gt;
&lt;li&gt;new users signing in using Twitter or Facebook accounts&lt;/li&gt;
&lt;li&gt;existing users signing in using Twitter or Facebook accounts&lt;/li&gt;
&lt;li&gt;existing users connecting their Twitter and Facebook accounts to their local account&lt;/li&gt;
&lt;li&gt;existing users disconnecting their Twitter and Facebook accounts from their local accounts&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;This app is set up to limit users to a one-to-one relationship between local accounts and social accounts from any provider. For example, you cannot link two Twitter accounts to your local account, and you cannot link your Twitter account to two local accounts. This is a subset of the functionality provided by Spring Social, which allows for many-to-many relationships between local and social accounts, but I think it addresses the most common use case.&lt;/p&gt;
&lt;p&gt;I plan to go over the demo application in more detail in a future post when I have more time. In the meantime, I will try to respond to questions or comments.&lt;/p&gt;</description><link>http://harmonicdevelopment.tumblr.com/post/20033907466</link><guid>http://harmonicdevelopment.tumblr.com/post/20033907466</guid><pubDate>Tue, 27 Mar 2012 16:58:16 -0700</pubDate></item><item><title>First of all thanks for your useful guide about integration of spring social and spring security. I'm facing some problems configuring ConnectionRepository and UserConnectionRepository in SocialConfig. I posted my problem on Stack Overflow (/questions/9462739/how-to-configure-spring-social). Please can you help me in some way?</title><description>&lt;p&gt;It looks like your question on Stack Overflow isn’t available anymore? If you have a new like I’d gladly try to answer your question there. Otherwise I am going to try to make available soon the code for a complete web app that uses Spring Social and Spring Security together. I will post about it when I’ve made the code available. Thanks for reading my posts!&lt;/p&gt;</description><link>http://harmonicdevelopment.tumblr.com/post/19773682436</link><guid>http://harmonicdevelopment.tumblr.com/post/19773682436</guid><pubDate>Thu, 22 Mar 2012 23:06:14 -0700</pubDate></item><item><title>Adding Spring Social to a Spring MVC and Spring Security Web App, Part 3</title><description>&lt;ul&gt;&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13613051804/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13626880413/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13664810119/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So far we have implemented and configured the classes for persisting social connection information for users, and tied Spring Social&amp;#8217;s Web support in to our sign in and sign up processes in Spring MVC and Spring Security. We have a few last changes to make to our Spring MVC, Java web app, and Spring Security  configuration. Then we&amp;#8217;ll actually make all this awesome new functionality available in the views of our application. Then we&amp;#8217;ll be done! Suddenly way more people will be using your Web application because they can sign in quickly and easily using their Facebook and Twitter accounts. And your app can get social by acting on behalf of your users on their social networks. And best of all it will be trivial for you to add support for other social network providers (e.g. LinkedIn, GitHub, Foursquare, etc.).&lt;/p&gt;
&lt;!-- more --&gt;
&lt;p&gt;&lt;strong&gt;Spring MVC Configuration Changes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You will need to modify your Spring MVC configuration to add the two controllers provided by Spring Social Web, ProviderSignInController and ConnectController. We&amp;#8217;ve already discussed ProviderSignInController. This controller handles the requests when users sign in to your app using Facebook/Twitter/etc. The ConnectController allows current users to associate their Facebook/Twitter accounts with their accounts in your application, so that they can then also sign in to your application using Facebook/Twitter.&lt;/p&gt;
&lt;p&gt;Here is some example XML for configuring these controllers. You could also configure them in a @Configuration class.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;  &amp;lt;bean class=&amp;#8221;org.springframework.social.connect.web.ConnectController&amp;#8221;&amp;gt;&lt;br/&gt;    &amp;lt;!&amp;#8212; relies on by-type autowiring for the constructor-args &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;property name=&amp;#8221;applicationUrl&amp;#8221; value=&amp;#8221;${site.url}&amp;#8221; /&amp;gt;&lt;br/&gt;  &amp;lt;/bean&amp;gt;&lt;br/&gt;&lt;br/&gt;  &amp;lt;bean class=&amp;#8221;org.springframework.social.connect.web.ProviderSignInController&amp;#8221;&amp;gt;&lt;br/&gt;    &amp;lt;!&amp;#8212; relies on by-type autowiring for the constructor-args &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;property name=&amp;#8221;applicationUrl&amp;#8221; value=&amp;#8221;${site.url}&amp;#8221; /&amp;gt;&lt;br/&gt;    &amp;lt;property name=&amp;#8221;signUpUrl&amp;#8221; value=&amp;#8221;/register&amp;#8221; /&amp;gt;&lt;br/&gt;    &amp;lt;property name=&amp;#8221;signInUrl&amp;#8221; value=&amp;#8221;/login&amp;#8221; /&amp;gt;&lt;br/&gt;  &amp;lt;/bean&amp;gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I recommend that you make the &amp;#8220;applicationUrl&amp;#8221; value a configurable property, so that you can use &amp;#8220;http://localhost:8080/context&amp;#8221; in development, and your application&amp;#8217;s real URL in production. Note that Facebook will not redirect users to &amp;#8220;localhost&amp;#8221;, so as far as I could figure it&amp;#8217;s not possible to test Facebook integration on localhost. (If it is possible, feel free to leave details in a comment.) You will also want to provide your application&amp;#8217;s sign up and sign in URLs (mine are &amp;#8220;/register&amp;#8221; and &amp;#8220;/login&amp;#8221;, as you can see above).&lt;/p&gt;
&lt;p&gt;You must also declare your SignInAdapter implementation bean. This should be straightforward as you should be able to have all their dependencies provided by autowiring.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;web.xml Changes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You may need to change your application&amp;#8217;s web.xml file. If you configure your Spring context using XML and you list your configuration XML files in your web.xml file using the &amp;#8220;contextConfigLocation&amp;#8221; context-param, you will need to add your social configuration XML file to the list. If you plan on allowing your users to remove associations between their Facebook/Twitter accounts and their accounts in their applications, you will also need to add a Spring filter, HiddenHttpMethodFilter. This filter enables support for HTTP methods beyond just GET and POST from Web browsers, which do not ordinarily support other HTTP methods. Other methods are simulated by providing a hidden input with the other method (e.g. DELETE) as the value. HiddenHttpMethodFilter will modify incoming requests so that your controller methods are invoked as if the browser used the correct HTTP method. Here is how you would declare that filter:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;  &amp;lt;filter&amp;gt;&lt;br/&gt;    &amp;lt;filter-name&amp;gt;hiddenHttpMethodFilter&amp;lt;/filter-name&amp;gt;&lt;br/&gt;    &amp;lt;filter-class&amp;gt;org.springframework.web.filter.HiddenHttpMethodFilter&amp;lt;/filter-class&amp;gt;&lt;br/&gt;  &amp;lt;/filter&amp;gt;&lt;br/&gt;&lt;br/&gt;  &amp;lt;filter-mapping&amp;gt;&lt;br/&gt;    &amp;lt;filter-name&amp;gt;hiddenHttpMethodFilter&amp;lt;/filter-name&amp;gt;&lt;br/&gt;    &amp;lt;url-pattern&amp;gt;/connect/*&amp;lt;/url-pattern&amp;gt;&lt;br/&gt;  &amp;lt;/filter-mapping&amp;gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You only need to apply it to the &amp;#8220;/connect/*&amp;#8221; url-pattern as it only applies to the ConnectController, which handles requests to that url-pattern.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Spring Security Configuration Changes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you are planning to allow your existing users to add and remove associations between their Facebook/Twitter accounts and their accounts in your application, you will want to make sure to secure requests to the &amp;#8220;/connect/*&amp;#8221; path so that they can only be made by logged in users. For example:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;    &amp;lt;intercept-url pattern=&amp;#8221;/connect/**&amp;#8221; access=&amp;#8221;IS_AUTHENTICATED_REMEMBERED&amp;#8221;/&amp;gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;#8220;&lt;strong&gt;Sign In With &amp;#8230;&lt;/strong&gt;&amp;#8221;&lt;strong&gt; View Changes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For your users to sign in with Facebook/Twitter, you will need to provide forms that POST to URLs of the format &amp;#8220;/signin/(provider name)&amp;#8221;, e.g. &amp;#8220;/signin/facebook&amp;#8221; and &amp;#8220;/signin/twitter&amp;#8221;, for Facebook and Twitter respectively.&lt;/p&gt;
&lt;p&gt;For Facebook signin, you will also need to add a hidden input named &amp;#8220;scope&amp;#8221; in which you provide a comma separated list of the Facebook permissions your application will need. You will almost certainly want to use the &amp;#8220;offline_access&amp;#8221; permission, which instructs Facebook to give your application an access token that will not expire. Other useful permissions include &amp;#8220;publish_stream&amp;#8221; (for posting to a user&amp;#8217;s wall) and &amp;#8220;manage_pages&amp;#8221; (for managing the pages to which a user has admin access).&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s an example Facebook sign in form:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;lt;form id=&amp;#8221;fb_signin&amp;#8221; action=&amp;#8221;&amp;lt;c:url value=&amp;#8221;/signin/facebook&amp;#8221;/&amp;gt;&amp;#8221; method=&amp;#8221;POST&amp;#8221;&amp;gt;&amp;lt;input&lt;br/&gt;                  type=&amp;#8221;hidden&amp;#8221; name=&amp;#8221;scope&amp;#8221; value=&amp;#8221;publish_stream,offline_access&amp;#8221;&amp;gt;&amp;lt;a&lt;br/&gt;                  href=&amp;#8221;javascript:document.forms.fb_signin.submit()&amp;#8221; title=&amp;#8221;Log In With Facebook&amp;#8221;&amp;gt;&amp;lt;img&lt;br/&gt;                  src=&amp;#8221;&amp;lt;c:url value=&amp;#8221;/img/facebook.gif&amp;#8221;/&amp;gt;&amp;#8221; width=&amp;#8221;14&amp;#8221; height=&amp;#8221;14&amp;#8221;/&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/form&amp;gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Showing Users Their Social Account Connection Status&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You may want to show your users when they have connected one of their social accounts to their account in your application. To do this for a user, you will want to use your UsersConnectionRepository implementation to create a ConnectionRepository for that user. Then you can retrieve a user&amp;#8217;s connections and populate model data for display in a view. Here is some example code for adding &amp;#8220;twitterConnected&amp;#8221; and &amp;#8220;facebookConnected&amp;#8221; booleans to the model that can be used to show a user whether or not they have connected their Twitter and Facebook accounts.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;    ConnectionRepository connectionRepository = socialUserService.createConnectionRepository(user.getUsername());&lt;br/&gt;    List connections = connectionRepository.findConnections(&amp;#8220;twitter&amp;#8221;);&lt;br/&gt;    if (!connections.isEmpty()) {&lt;br/&gt;      model.addAttribute(&amp;#8220;twitterConnected&amp;#8221;, true);&lt;br/&gt;    }&lt;br/&gt;    connections = connectionRepository.findConnections(&amp;#8220;facebook&amp;#8221;);&lt;br/&gt;    if (!connections.isEmpty()) {&lt;br/&gt;      model.addAttribute(&amp;#8220;facebookConnected&amp;#8221;, true);&lt;br/&gt;    }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Enabling Existing Users to Connect/Disconnect Social Accounts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;re showing your users their social account connection status, you may also allow them to connect and disconnect their social accounts too. That way existing users of your application can then sign in using their social accounts, and your application can act on their behalf on their social networks.&lt;/p&gt;
&lt;p&gt;This is done by using a form that makes a POST to &amp;#8220;/connect/(provider id)&amp;#8221;, e.g. &amp;#8220;/connect/facebook&amp;#8221; and &amp;#8220;/connect/twitter&amp;#8221;. In the case of connecting an account, you would also potentially need to include the hidden &amp;#8220;scope&amp;#8221; input mentioned previously, where you would list permissions that your application wants (e.g. &amp;#8220;offline_access&amp;#8221; for Facebook). In the case of disconnecting an account, you would need to add the hidden &amp;#8220;_method&amp;#8221; input with a value of &amp;#8220;delete&amp;#8221;. This will be picked up by the HiddenHttpMethodFilter mentioned earlier so that the POST request actually gets handled by a controller method expecting a DELETE request. Here are some example forms:&lt;/p&gt;
&lt;p&gt;Connect:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;lt;form id=&amp;#8221;fb_signin&amp;#8221; action=&amp;#8221;&amp;lt;c:url value=&amp;#8221;/connect/facebook&amp;#8221;/&amp;gt;&amp;#8221; method=&amp;#8221;POST&amp;#8221;&amp;gt;&lt;br/&gt;&amp;lt;input type=&amp;#8221;hidden&amp;#8221; name=&amp;#8221;scope&amp;#8221; value=&amp;#8221;publish_stream,offline_access&amp;#8221;&amp;gt;&lt;br/&gt;&amp;lt;input type=&amp;#8221;submit&amp;#8221; name=&amp;#8221;submitBtn&amp;#8221; value=&amp;#8221;Connect&amp;#8221;&amp;gt;&lt;br/&gt;&amp;lt;/form&amp;gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Disconnect:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;lt;form id=&amp;#8221;fb_signin&amp;#8221; action=&amp;#8221;&amp;lt;c:url value=&amp;#8221;/connect/facebook&amp;#8221;/&amp;gt;&amp;#8221; method=&amp;#8221;POST&amp;#8221;&amp;gt;&lt;br/&gt;&amp;lt;input type=&amp;#8221;hidden&amp;#8221; name=&amp;#8221;_method&amp;#8221; value=&amp;#8221;delete&amp;#8221;&amp;gt;&lt;br/&gt;&amp;lt;input type=&amp;#8221;submit&amp;#8221; name=&amp;#8221;submitBtn&amp;#8221; value=&amp;#8221;Disconnect&amp;#8221;&amp;gt;&lt;br/&gt;&amp;lt;/form&amp;gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;When users submit these forms, the ConnectController will send them to views named &amp;#8220;connect/(provider id)Connected&amp;#8221; when connecting an account and &amp;#8220;connect/(provider id)Connect&amp;#8221; when disconnecting an account. You will need to create these two views for every provider your app supports.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;OMG, You&amp;#8217;re Done!!!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Whew! It took a long way to get here, but you are finally done adding Spring Social to your Spring MVC and Spring Security Web app. Awesome!&lt;/p&gt;
&lt;p&gt;The first time you go through this process, it can be a bit overwhelming, but here are two great things about using Spring Social: 1) Once you&amp;#8217;ve done this process in one app, you can just repeat it to add social features to other apps, and it gets much easier every time you do it. 2) You can add support for new social providers &lt;em&gt;very&lt;/em&gt; quickly and easily, if there are Spring Social implementations for that provider. The list of supported providers is already pretty long and it&amp;#8217;s growing all the time thanks to an active community. Another great aspect of Spring Social is how makes it relatively straightforward to write code supporting a new provider. I myself wrote a &lt;a href="http://github.com/sdouglass/spring-social-tumblr" target="_blank"&gt;Spring Social Tumblr implementation&lt;/a&gt; without too much difficulty (other than dealing with some non-standard aspects of Tumblr&amp;#8217;s API). Maybe I will describe that process in a future post.&lt;/p&gt;
&lt;p&gt;Thanks for reading along. Good luck and good coding!&lt;/p&gt;</description><link>http://harmonicdevelopment.tumblr.com/post/13664810119</link><guid>http://harmonicdevelopment.tumblr.com/post/13664810119</guid><pubDate>Fri, 02 Dec 2011 20:21:00 -0800</pubDate><category>spring social mvc security twitter facebook oauth</category></item><item><title>Adding Spring Social to a Spring MVC and Spring Security Web App, Part 2</title><description>&lt;ul&gt;&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13613051804/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13626880413/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13664810119/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;In Part I of this series, we covered adding the jars/dependencies needed to start using Spring Social in a Spring MVC and Spring Security Web application. We also covered the classes involved in persisting users&amp;#8217; social connection information.&lt;/p&gt;
&lt;p&gt;Now we&amp;#8217;ll configure our Spring Social related beans. Then we&amp;#8217;ll start modifying the MVC configuration and classes to start making Spring Social functionality available to users.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;!-- more --&gt;Configuring Spring Social Beans&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are several beans you will need to configure, either using Spring&amp;#8217;s traditional XML configuration, or its more recently developed Java configuration. You can find some good example Java and XML configuration in &lt;a href="http://static.springsource.org/spring-social/docs/1.0.x/reference/html/connecting.html" target="_blank"&gt;the reference documentation&lt;/a&gt;. I&amp;#8217;m just going to go over the beans you&amp;#8217;ll need to configure:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;ConnectionFactoryLocator - this is the central bean of Spring Social; you must register all provider specific connection factories with this bean&lt;/li&gt;
&lt;li&gt;FacebookConnectionFactory, TwitterConnectionFactory, etc. - provider specific ConnectionFactory implementations; these take the OAuth consumer key and secret for your Facebook, Twitter, etc. applications; you should provide those key/secret values from a properties file using a PropertyPlaceholderConfigurer&lt;/li&gt;
&lt;li&gt;UsersConnectionRepository - either the included JdbcUsersConnectionRepository or your own implementation&lt;/li&gt;
&lt;li&gt;ConnectionRepository - the request scoped bean for logged in users created by UsersConnectionRepository.createConnectionRepository() (see &lt;a href="http://static.springsource.org/spring-social/docs/1.0.x/reference/html/connecting.html#section_connecting_service_providers" target="_blank"&gt;the reference documentation&lt;/a&gt; for how to use Spring Expression Language to access the currently logged in user&amp;#8217;s username)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;You&amp;#8217;ll want to configure these beans in a new XML file or @Configuration class, and add that file or class to your application context. (For example, you could add it to a &amp;#8220;myapp-social.xml&amp;#8221; file and add that file to the list of config files in your &amp;#8220;contextConfigLocation&amp;#8221; context-param value in your web.xml.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Class to Support Signing In With Facebook/Twitter&lt;br/&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now we start on the meat of the integration between Spring Social and your Spring MVC and Spring Security app.&lt;/p&gt;
&lt;p&gt;First we will work on making it possible for users to sign in using Facebook/Twitter/etc. Spring Social Web provides a controller, ProviderSignInController, and an interface, SignInAdapter, that allow you to tie the controller in to your application&amp;#8217;s sign in process. The SignInAdapter implementation is what will talk to Spring Security to sign a user in to your application (complete with optional &amp;#8220;remember-me&amp;#8221; support).&lt;/p&gt;
&lt;p&gt;When a user tries to sign in using Facebook/Twitter/etc., ProviderSignInController will check if there is an existing local user associated with the retrieved provider id and provider user id. If an associated local user is found, the controller will call the SignInAdapter implementation with the local user id, the Connection&amp;lt;?&amp;gt; instance, and a NativeWebRequest instance.&lt;/p&gt;
&lt;p&gt;Here is my example SignInAdapter. Yours may be slightly different.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;public class SignInAdapterImpl implements SignInAdapter {&lt;br/&gt;  private UserService userService; // injected&lt;br/&gt;  private TokenBasedRememberMeServices tokenBasedRememberMeServices; // injected&lt;br/&gt;&lt;br/&gt;  public String signIn(String userId, Connection&amp;lt;?&amp;gt; connection, NativeWebRequest request) {&lt;br/&gt;    User user = userService.findByLogin(userId);&lt;br/&gt;    Authentication authentication = SecurityUtil.signInUser(user);&lt;br/&gt;    // set remember-me cookie&lt;br/&gt;    tokenBasedRememberMeServices.onLoginSuccess(&lt;br/&gt;        (HttpServletRequest) request.getNativeRequest(),&lt;br/&gt;        (HttpServletResponse) request.getNativeResponse(),&lt;br/&gt;        authentication);&lt;br/&gt;    return null;&lt;br/&gt;  }&lt;br/&gt;&amp;#8230;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;There are three basic steps being taken in this code:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;look up your local user by the given local user id&lt;/li&gt;
&lt;li&gt;log the user in to the Spring Security context&lt;/li&gt;
&lt;li&gt;set a Spring Security remember-me cookie (this part is optional)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Here is the code from my SecurityUtil.signInUser() method. The basic idea is to create an Authentication instance and set it in the SecurityContext.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;  public static Authentication signInUser(User user) {&lt;br/&gt;    List&amp;lt;GrantedAuthority&amp;gt; authorities = UserDetailsServiceImpl.createAuthorities(user);&lt;br/&gt;    SpringSecurityUser springSecurityUser = new SpringSecurityUser(user, authorities);&lt;br/&gt;    Authentication authentication = new UsernamePasswordAuthenticationToken(springSecurityUser, user.getPassword(), authorities);&lt;br/&gt;    SecurityContextHolder.getContext().setAuthentication(authentication);&lt;br/&gt;    return authentication;&lt;br/&gt;  }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If a local user associated with the provider id and provider user id cannot be found, users will be redirected to a configurable sign up URL. You can also specify a different URL using the return value of SignInAdapter.signIn() (I&amp;#8217;m returning null, indicating that the controller should use its signInUrl property.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Automatic Sign Up / Registration&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you would prefer new users who try to sign in with Facebook/Twitter to be registered automatically, rather than sent to a sign up URL, you will need to make sure that the ProviderSignInController always finds a local user account associated with the provider id and provider user id from a sign in request.&lt;/p&gt;
&lt;p&gt;ProviderSignInController calls UserConnectionRepository.findUserIdsWithConnection() and passes in the Connection when checking for associated local users. Inside this method you will need to create a local user account if none is found for the connection.&lt;/p&gt;
&lt;p&gt;If you are using the JdbcUsersConnectionRepository class provided by Spring Social, you need to implement the ConnectionSignUp interface. This interface is used in JdbcUsersConnectionRepository.findUserIdsWithConnection() when no user ids are found for a given Connection.&lt;/p&gt;
&lt;p&gt;If you have your own UsersConnectionRepository implementation, you do not necessarily need to implement ConnectionSignUp. You just need to modify the code in your &amp;#8220;findUserIdsWithConnection()&amp;#8221; implementation. You could use ConnectionSignUp the same way that the JdbcUsersConnectionRepository does if you want to, though.&lt;/p&gt;
&lt;p&gt;Either way, you will need to write some code that translates the Connection, and probably also the user&amp;#8217;s profile info from the provider, into a local user object and persist the new local user. In the case of implementing ConnectionSignUp you would then return the local unique username of the new user.&lt;/p&gt;
&lt;p&gt;The user profile can be retrieved from the provider by calling &amp;#8220;fetchUserProfile()&amp;#8221; on the Connection. Be aware that different providers return different profile data. Twitter, for example, does not return an email address. You may want to perform whatever validation you normally perform during a form-based sign-up to make sure that all the data required by your app are present. If you are implementing ConnectionSignUp and you cannot automatically register the user for whatever reason, you should return null, indicating that no new user was created.&lt;/p&gt;
&lt;p&gt;If you do implement ConnectionSignUp you will likely want to declare that bean in your Spring Social Java or XML configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sign Up Controller Modification&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you need to handle the case when a user tries to sign in via Facebook/Twitter and the sign in fails (because no local user is found, and you either have not enabled automatic registration, or automatic registration failed), you will need to modify your sign up controller. You will need to add code like this to your GET request handler method:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;// check for failed sign in/up from facebook/twitter&lt;br/&gt;Connection&amp;lt;?&amp;gt; connection = ProviderSignInUtils.getConnection(request);&lt;br/&gt;if (connection&amp;#160;!= null) {&lt;br/&gt;  // user tried signing in/up with but they could not be signed in or signed up automatically&lt;br/&gt;&lt;br/&gt;  // present the user with a meaningful error&lt;br/&gt;  // errors.reject(&amp;#8220;failedSocialSignIn&amp;#8221;, &amp;#8220;error message goes here&amp;#8221;);&lt;br/&gt;&lt;br/&gt;  UserProfile userProfile = connection.fetchUserProfile();&lt;br/&gt;  // copy connection and profile data to your sign up form bean&lt;br/&gt;  // validate the data and put errors in the model to display to the user&lt;br/&gt;}&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You will probably need to add WebRequest and BindingResult arguments to your GET request method handler.&lt;/p&gt;
&lt;p&gt;You will also need to modify your POST request method handler, to complete the social sign in/up process:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;// check for failed sign in/up from facebook/twitter&lt;br/&gt;Connection&amp;lt;?&amp;gt; connection = ProviderSignInUtils.getConnection(request);&lt;br/&gt;if (connection&amp;#160;!= null) {&lt;br/&gt;  // finish social signup/login if there is one&lt;br/&gt;  ProviderSignInUtils.handlePostSignUp(user.getUsername(), request);&lt;br/&gt;}&lt;br/&gt;// you can then either sign the user in immediately (e.g. using your SignInAdapter implementation)&lt;br/&gt;// or send them to the success view of the registration process per normal&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You will probably need to add a NativeWebRequest argument to your POST request method handler.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Next Time: Changing Web and Security Config, and the View &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Whew! That was intense. The good news is you are almost done. Next time we will cover changing your Spring MVC config, your web.xml, and your Spring Security config. Then lastly we will cover changes to your views. And then you&amp;#8217;ll be done. Hooray! Hang in there.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13664810119/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;On to Part 3!&lt;br/&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://harmonicdevelopment.tumblr.com/post/13626880413</link><guid>http://harmonicdevelopment.tumblr.com/post/13626880413</guid><pubDate>Thu, 01 Dec 2011 23:08:00 -0800</pubDate><category>spring social mvc security twiter facebook oauth</category></item><item><title>Adding Spring Social to a Spring MVC and Spring Security Web App, Part 1</title><description>&lt;ul&gt;&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13613051804/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13626880413/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13664810119/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;Part 3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;This series of posts will explain step by step how to add &lt;a href="http://www.springsource.org/spring-social" title="Spring Social" target="_blank"&gt;Spring Social&lt;/a&gt; to an existing Web application that uses &lt;a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html" target="_blank"&gt;Spring MVC&lt;/a&gt; and &lt;a href="http://static.springsource.org/spring-security/site/" target="_blank"&gt;Spring Security&lt;/a&gt;. You will add support for new users to sign in to your application using their Facebook/Twitter accounts. You will add support for current users to associate their Facebook/Twitter accounts with their accounts in your application, so that they too can log in to your application using their Facebook/Twitter accounts. Your application can then also act on a user&amp;#8217;s behalf on Facebook/Twitter.&lt;/p&gt;
&lt;p&gt;Much of this information is drawn from the &lt;a href="http://static.springsource.org/spring-social/docs/1.0.x/reference/html/" target="_blank"&gt;Spring Social Core reference documentation&lt;/a&gt;. I found myself jumping back and forth between my code and that documentation and feeling like, while the documentation did present most of the necessary information, it did not necessarily present that information in the order in which it became relevant to me during the process of integrating Spring Social into an app. So, I thought I would try to publish a step by step guide to supplement the documentation.&lt;/p&gt;
&lt;p&gt;I am going to include information for incorporating both Facebook and Twitter support. If you would like to just include support for one or the other, please note that you will have to edit the example configurations and code appropriately.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;p&gt;&lt;strong&gt;Add Core Dependencies&lt;br/&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;First you will need to add the Spring Social jar files to your project. I use &lt;a href="http://maven.apache.org/" target="_blank"&gt;Maven&lt;/a&gt; to manage the dependencies of my app. If you do not, you will need to go to the &lt;a href="http://www.springsource.org/spring-social" target="_blank"&gt;Spring Social&lt;/a&gt; site, download the core, Twitter, and Facebook archives, decompress them, and copy the jar files into your application&amp;#8217;s lib directory.&lt;/p&gt;
&lt;p&gt;If you do use Maven, you will need to make the following changes to your pom.xml file:&lt;/p&gt;
&lt;p&gt;1. I recommend you first add properties for the version numbers for the various libraries:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;  &amp;lt;properties&amp;gt;&lt;br/&gt;    &amp;lt;spring-social.version&amp;gt;1.0.0.RELEASE&amp;lt;/spring-social.version&amp;gt;&lt;br/&gt;    &amp;lt;spring-social-twitter.version&amp;gt;1.0.0.RELEASE&amp;lt;/spring-social-twitter.version&amp;gt;&lt;br/&gt;    &amp;lt;spring-social-facebook.version&amp;gt;1.0.0.RELEASE&amp;lt;/spring-social-facebook.version&amp;gt;&lt;br/&gt;  &amp;lt;/properties&amp;gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Note that the Facebook and Twitter libraries are on their own release schedules and will not necessarily always have the same version number as the core Spring Social libraries. Hence, they should have their own version properties.&lt;/p&gt;
&lt;p&gt;2. Add the following dependencies:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;    &amp;lt;!&amp;#8212; Spring Social Core &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;dependency&amp;gt;&lt;br/&gt;      &amp;lt;groupId&amp;gt;org.springframework.social&amp;lt;/groupId&amp;gt;&lt;br/&gt;      &amp;lt;artifactId&amp;gt;spring-social-core&amp;lt;/artifactId&amp;gt;&lt;br/&gt;      &amp;lt;version&amp;gt;${spring-social.version}&amp;lt;/version&amp;gt;&lt;br/&gt;    &amp;lt;/dependency&amp;gt;&lt;br/&gt;    &amp;lt;!&amp;#8212; Spring Social Web (contains login/signup controllers) &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;dependency&amp;gt;&lt;br/&gt;      &amp;lt;groupId&amp;gt;org.springframework.social&amp;lt;/groupId&amp;gt;&lt;br/&gt;      &amp;lt;artifactId&amp;gt;spring-social-web&amp;lt;/artifactId&amp;gt;&lt;br/&gt;      &amp;lt;version&amp;gt;${spring-social.version}&amp;lt;/version&amp;gt;&lt;br/&gt;    &amp;lt;/dependency&amp;gt;&lt;br/&gt;    &amp;lt;!&amp;#8212; Spring Social Twitter &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;dependency&amp;gt;&lt;br/&gt;      &amp;lt;groupId&amp;gt;org.springframework.social&amp;lt;/groupId&amp;gt;&lt;br/&gt;      &amp;lt;artifactId&amp;gt;spring-social-twitter&amp;lt;/artifactId&amp;gt;&lt;br/&gt;      &amp;lt;version&amp;gt;${spring-social-twitter.version}&amp;lt;/version&amp;gt;&lt;br/&gt;    &amp;lt;/dependency&amp;gt;&lt;br/&gt;    &amp;lt;!&amp;#8212; Spring Social Facebook &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;dependency&amp;gt;&lt;br/&gt;      &amp;lt;groupId&amp;gt;org.springframework.social&amp;lt;/groupId&amp;gt;&lt;br/&gt;      &amp;lt;artifactId&amp;gt;spring-social-facebook&amp;lt;/artifactId&amp;gt;&lt;br/&gt;      &amp;lt;version&amp;gt;${spring-social-facebook.version}&amp;lt;/version&amp;gt;&lt;br/&gt;    &amp;lt;/dependency&amp;gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Optional Dependency: Spring Security Crypto&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you plan to use Twitter or any other OAuth1 service provider, or you plan to encrypt the OAuth token/secret values of your users in your database (which is a good idea), you&amp;#8217;ll want to also include the Spring Security Crypto library. Unfortunately, this library is only available in Spring Security 3.1 which has not had a final release yet. If you are not using Maven, you will need to get the jars from the &lt;a href="http://static.springsource.org/spring-security/site/downloads.html" target="_blank"&gt;Spring Security downloads page&lt;/a&gt;. If you are using Maven, you will first need to add the following repository:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;  &amp;lt;repositories&amp;gt;&lt;br/&gt;    &amp;lt;repository&amp;gt;&lt;br/&gt;        &amp;lt;id&amp;gt;org.springframework.maven.milestone&amp;lt;/id&amp;gt;&lt;br/&gt;        &amp;lt;name&amp;gt;Spring Maven Milestone Repository&amp;lt;/name&amp;gt;&lt;br/&gt;        &amp;lt;url&amp;gt;http://maven.springframework.org/milestone&amp;lt;/url&amp;gt;&lt;br/&gt;    &amp;lt;/repository&amp;gt;&lt;br/&gt;  &amp;lt;/repositories&amp;gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Then with that repository in place you can add the Spring Security Crypto dependency:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;    &amp;lt;!&amp;#8212; Spring Security Crypto, required if you use: &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;!&amp;#8212;   any OAuth1 service provider (e.g. Twitter) &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;!&amp;#8212;   the provided JDBC connection repository classes &amp;#8212;&amp;gt;&lt;br/&gt;    &amp;lt;dependency&amp;gt;&lt;br/&gt;      &amp;lt;groupId&amp;gt;org.springframework.security&amp;lt;/groupId&amp;gt;&lt;br/&gt;      &amp;lt;artifactId&amp;gt;spring-security-crypto&amp;lt;/artifactId&amp;gt;&lt;br/&gt;      &amp;lt;version&amp;gt;3.1.0.RC3&amp;lt;/version&amp;gt;&lt;br/&gt;    &amp;lt;/dependency&amp;gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Social User Connection Entity&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;OK! The first thing you will want to do now that you&amp;#8217;ve added your dependencies is figure out how you are going to persist social connection information for your users.&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://static.springsource.org/spring-social/docs/1.0.x/reference/html/serviceprovider.html#service-providers-persisting-connections-jdbc" target="_blank"&gt;reference documentation&lt;/a&gt; explains that Spring Social Core comes with classes for persisting connection info to a relational database using JDBC. A SQL file is provided in the core jar which you can use to create the necessary table. If you use the SQL and provided classes you do not need to write an entity class to model that data.&lt;/p&gt;
&lt;p&gt;I personally use JPA annotations and Hibernate (managed by Spring). So I went ahead and wrote an entity class to model the social connection data:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;@Entity&lt;br/&gt;@Table(uniqueConstraints = {@UniqueConstraint(columnNames = { &amp;#8220;userId&amp;#8221;, &amp;#8220;providerId&amp;#8221;, &amp;#8220;providerUserId&amp;#8221; }),&lt;br/&gt;                            @UniqueConstraint(columnNames = { &amp;#8220;userId&amp;#8221;, &amp;#8220;providerId&amp;#8221;, &amp;#8220;rank&amp;#8221; })})&lt;br/&gt;public class SocialUser {&lt;br/&gt;&lt;br/&gt;  @Id&lt;br/&gt;  @GeneratedValue(strategy = GenerationType.AUTO)&lt;br/&gt;  private int id;&lt;br/&gt;&lt;br/&gt;  /**&lt;br/&gt;   * A local identifier for the user, in our case the username.&lt;br/&gt;   */&lt;br/&gt;  private String userId;&lt;br/&gt;&lt;br/&gt;  @Column(nullable = false)&lt;br/&gt;  private String providerId;&lt;br/&gt;&lt;br/&gt;  private String providerUserId;&lt;br/&gt;&lt;br/&gt;  @Column(nullable = false)&lt;br/&gt;  private int rank;&lt;br/&gt;&lt;br/&gt;  private String displayName;&lt;br/&gt;&lt;br/&gt;  private String profileUrl;&lt;br/&gt;&lt;br/&gt;  private String imageUrl;&lt;br/&gt;&lt;br/&gt;  @Column(nullable = false)&lt;br/&gt;  private String accessToken;&lt;br/&gt;&lt;br/&gt;  private String secret;&lt;br/&gt;&lt;br/&gt;  private String refreshToken;&lt;/p&gt;
&lt;p&gt;  private Long expireTime;&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;  private Date createDate = new Date();&lt;/p&gt;
&lt;p&gt;&amp;#8230;&lt;/p&gt;
&lt;p&gt;// getters and setters omitted for brevity&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This class is based on the SQL definition provided in the Spring Social reference documentation, with the addition of a numerical auto-generated primary key (mostly out of habit I guess), and a create date timestamp for my own information.&lt;/p&gt;
&lt;p&gt;Here is a brief overview of the fields:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;userId - the reference documentation is not very specific about what value should  be used in the &amp;#8220;userId&amp;#8221; column/field, but based on feedback from the &lt;a href="http://forum.springsource.org/forumdisplay.php?82-Social" target="_blank"&gt;Spring Social forum&lt;/a&gt; and from trial and error I suggest using your user&amp;#8217;s unique username in  your application&lt;/li&gt;
&lt;li&gt;providerId - this is the string provider id value, e.g. &amp;#8220;facebook&amp;#8221;, &amp;#8220;twitter&amp;#8221;, etc.&lt;/li&gt;
&lt;li&gt;providerUserId - this is the user&amp;#8217;s unique id in the provider&amp;#8217;s system&lt;/li&gt;
&lt;li&gt;rank - Spring Social actually allows for 1-n accounts per provider per user (e.g. multiple Facebook accounts associated with one user in your application), and this value determines the order of importance of those accounts; generally though there will just be 1 account per provider per user and this value will generally be 1&lt;/li&gt;
&lt;li&gt;displayName, profileUrl, imageUrl - some profile data fields that may or may not be sent to your application by the provider&lt;/li&gt;
&lt;li&gt;accessToken, secret, refreshToken, expireTime - these are the OAuth credentials and related information, and you will likely want to encrypt the accessToken and secret values in your database&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;strong&gt;Implement Connection Persistence Interfaces&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As &lt;a href="http://static.springsource.org/spring-social/docs/1.0.x/reference/html/serviceprovider.html#service-providers-persisting-connections" target="_blank"&gt;mentioned in the reference documentation&lt;/a&gt; there are two interfaces involved in connection persistence:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;ConnectionRepository - handles connection persistence methods for one specific user; the implementation bean will be request scoped, created for logged in users of your application&lt;/li&gt;
&lt;li&gt;UsersConnectionRepository - handles connection persistence methods across all users; this will be a normal singleton bean in your application context&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Spring Social Core comes with implementations that work with a  relational database using JDBC. If you use those implementations,  you do not need to write your own implementations of these interfaces. More information will be presented later about configuring your app to use those classes.&lt;/p&gt;
&lt;p&gt;If you use JPA for your persistence code in your application, you may want to look at &lt;a href="https://github.com/mschipperheyn/spring-social-jpa" target="_blank"&gt;this user contributed Spring Social JPA project&lt;/a&gt;. I attempted to use that code but felt like it didn&amp;#8217;t match up well with how I had structured the code in my app. However, it was a good starting point for my own implementations of ConnectionRepository and UsersConnectionRepository. There are a lot of queries involved and you also have to do some transformation of method arguments and query results to get things working, and some of that work has been done for you in the JPA project.&lt;/p&gt;
&lt;p&gt;I personally use Hibernate and Spring for my persistence code, so I wrote a SocialUserDAO interface and implementation. Here are the important finder methods from the DAO:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;  List&amp;lt;SocialUser&amp;gt; findByUserId(String userId);&lt;br/&gt;&lt;br/&gt;  List&amp;lt;SocialUser&amp;gt; findByUserIdAndProviderId(String userId, String providerId);&lt;br/&gt;&lt;br/&gt;  List&amp;lt;SocialUser&amp;gt; findByUserIdAndProviderUserIds(String userId, MultiValueMap&amp;lt;String, String&amp;gt; providerUserIds);&lt;br/&gt;&lt;br/&gt;  SocialUser get(String userId, String providerId, String providerUserId);&lt;br/&gt;&lt;br/&gt;  List&amp;lt;SocialUser&amp;gt; findPrimaryByUserIdAndProviderId(String userId, String providerId);&lt;br/&gt;&lt;br/&gt;  Integer selectMaxRankByUserIdAndProviderId(String userId, String providerId);&lt;br/&gt;&lt;br/&gt;  List&amp;lt;String&amp;gt; findUserIdsByProviderIdAndProviderUserId(String providerId, String providerUserId);&lt;br/&gt;&lt;br/&gt;  List&amp;lt;String&amp;gt; findUserIdsByProviderIdAndProviderUserIds(String providerId, Set&amp;lt;String&amp;gt; providerUserIds);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Implementing most of these is fairly straightforward, with the exception of &amp;#8220;findByUserIdAndProviderUserIds()&amp;#8221;. Make sure that you get the boolean logic correct. You should use something like this pseudo query: &amp;#8220;where userId = (userId) AND ((providerId = providerId1 AND providerUserId = providerUserId1) OR (providerId = providerId2 AND providerUserId = providerUserId2) &amp;#8230; etc.)&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Next I wrote an implementation of ConnectionRepository. This is the request scoped bean for logged in users, for working with a single user&amp;#8217;s Spring Social connection information. This bean will be instantiated by the UsersConnectionRepository and will have its dependencies passed to its constructor.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;public class SocialUserConnectionRepositoryImpl implements ConnectionRepository {&lt;br/&gt;&lt;br/&gt;  private String userId;&lt;br/&gt;  private SocialUserDAO socialUserDAO;&lt;br/&gt;  private ConnectionFactoryLocator connectionFactoryLocator;&lt;br/&gt;  private TextEncryptor textEncryptor;&lt;br/&gt;&lt;br/&gt;  public SocialUserConnectionRepositoryImpl(String userId, SocialUserDAO socialUserDAO,&lt;br/&gt;                                            ConnectionFactoryLocator connectionFactoryLocator,&lt;br/&gt;                                            TextEncryptor textEncryptor) {&lt;br/&gt;    this.userId = userId;&lt;br/&gt;    this.socialUserDAO = socialUserDAO;&lt;br/&gt;    this.connectionFactoryLocator = connectionFactoryLocator;&lt;br/&gt;    this.textEncryptor = textEncryptor;&lt;br/&gt;  }&lt;/p&gt;
&lt;p&gt;&amp;#8230;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Here&amp;#8217;s a quick rundown of the fields/dependencies:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;userId - the userId of the logged in user (as previously stated, this should be the unique username of the user in your application)&lt;/li&gt;
&lt;li&gt;socialUserDAO - this is my DAO class, this may be different for you depending on how you handle your persistence&lt;/li&gt;
&lt;li&gt;connectionFactoryLocator - this is the core interface of Spring Social, it provides access to connection factories for all the providers (e.g. Facebook, Twitter, etc.) that you have configured for your app&lt;/li&gt;
&lt;li&gt;textEncryptor - a Spring Security Crypto class for encrypting/decrypting OAuth credentials stored in your database&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Given these (or equivalent) dependencies, implementing the ConnectionRepository interface is relatively straightforward. You will likely need to write a few methods in your class to perform the following tasks:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;creating a ConnectionData instance based on an instance of your local social user entity class (this is where you&amp;#8217;d decrypt OAuth credentials)&lt;/li&gt;
&lt;li&gt;creating a local social user entity instance based on a ConnectionData instance (this is where you&amp;#8217;d encrypt OAuth credentials)&lt;/li&gt;
&lt;li&gt;using the ConnectionFactoryLocator to create a Connection&amp;lt;?&amp;gt; instance from a ConnectionData instance&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Next up comes implementing UsersConnectionRepository. Fortunately this interface only has three methods and is much simpler to implement than ConnectionRepository.&lt;/p&gt;
&lt;p&gt;Your implementation should have the following or equivalent fields/dependencies:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;a DAO or other class for your social user entity data access methods&lt;/li&gt;
&lt;li&gt;an encryption password, provided from a configuration file, used to initialize the TextEncryptor&lt;/li&gt;
&lt;li&gt;a ConnectionFactoryLocator and a TextEncryptor (these will just be used as constructor arguments when instantiating your ConnectionRepository implementation for users)&lt;/li&gt;
&lt;li&gt;you may also want a configurable boolean that turns encryption/decryption on and off so that you can disable encryption of OAuth tokens in development&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Here is an example of how you can initialize your TextEncryptor instance in code (instead of XML) if you so choose:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;  @PostConstruct&lt;br/&gt;  public void initializeTextEncryptor() {&lt;br/&gt;    textEncryptor = Encryptors.text(encryptionPassword, KeyGenerators.string().generateKey());&lt;br/&gt;  }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Next Time: Spring Social, MVC, and Security Configuration&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s all for Part 2. Next time we will pick up with configuration of your ConnectionRepository and UsersConnectionRepository implementations, as well as the core Spring Social class, ConnectionFactoryLocator, and ConnectionFactory implementations for Facebook and Twitter. From there, we&amp;#8217;ll implement two more interfaces, for Spring Social Web, and begin modifying the MVC portion of our app.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://harmonicdevelopment.tumblr.com/post/13626880413/adding-spring-social-to-a-spring-mvc-and-spring" target="_self"&gt;On to Part 2!&lt;br/&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://harmonicdevelopment.tumblr.com/post/13613051804</link><guid>http://harmonicdevelopment.tumblr.com/post/13613051804</guid><pubDate>Thu, 01 Dec 2011 17:08:00 -0800</pubDate><category>spring social mvc security twiter facebook oauth</category></item></channel></rss>
