Monday, December 10, 2012

Spring Social with JavaConfig (Part 3)


In the previous section, we have shown the steps on how to generate and retrieve the OAuth secret keys from Facebook and Twitter. In this section we will setup the Spring Social configuration settings through JavaConfig.

Table of Contents

Click on a link to jump to that section:
  1. Functional Specs
  2. Generate OAuth keys
    • Facebook
    • Twitter
  3. Spring Social configuration
  4. Spring Security configuration
  5. JavaConfig
  6. View with Thymeleaf
  7. Layers
    • Domain
    • Repository
    • Service
    • Controller
  8. Running the application
    • Clone from GitHub
    • Create the Database
    • Run with Maven and Tomcat 7
    • Run with Maven and Jetty 8
    • Import to Eclipse
    • Validate with W3C

Spring Social configuration

What is Spring Social?

Spring Social is an extension of the Spring Framework that allows you to connect your applications with Software-as-a-Service (SaaS) providers such as Facebook and Twitter.

  • An extensible service provider framework that greatly simplifies the process of connecting local user accounts to hosted provider accounts.
  • A connect controller that handles the authorization flow between your Java/Spring web application, a service provider, and your users.
  • Java bindings to popular service provider APIs such as Facebook, Twitter, LinkedIn, TripIt, and GitHub.
  • A sign-in controller that enables users to authenticate with your application by signing in through a service provider.

Here's our Spring Social configuration:

Let me explain the contents of this configuration:

  • We have autowired the environment properties and the datasource
  • We have declared a ConnectionFactoryLocator which allows us to register connections to Facebook and Twitter. Notice how we passed the OAuth secret IDs and secret keys to the locator
  • We've declared a TextEncryptor for encrypting strings. This is required by Spring Social's JdbcUsersConnectionRepository
  • JdbcUsersConnectionRepository is used for persisting connections to a database through JDBC
  • ConnectionRepository allows a specific user to save and retrieve connections. We need to use this in conjunction with Spring Security because it provides us ready-made authenticated users. Notice how we assigned the current authenticated user
  • ConnectController is a controller for managing the connection flow to social media sites
  • HiddenHttpMethodFilter is required by Spring Social so that users can disconnect from social media sites. The filter needs to be declared in the web.xml or ApplicationInitializer

Note: If you need an in-depth explanation of each classes, please see the official Spring Social docs


In the next section, we will focus on Spring Security-related configuration. Click here to proceed.
StumpleUpon DiggIt! Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring Social with JavaConfig (Part 3) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share


  1. Hi Mark,
    I guess HiddenHttpMethodFilter is used for the following purpose:

    If the client of your RESTful solution is a browser, not a costumed web client, then please beware that HTML only supports “GET” and “POST” methods, i.e. browsers might not know how to handle HTML form, whose action method is “PUT” or “DELETE”. To get around this, you can add a hidden filed in your web form to pass the “real” method type back to your web application. That is how Spring does it. They add a hidden “_method” parameter. And to make our life easier, Spring provides the HiddenHttpMethodFilter, which interprets and converts the hidden paramter into the corresponding HTTP method request. To do so, simply add this filter to your web.xml and map it to your Spring servlet.

    PS: I copied this line from a blog.

  2. how does the UsersConnectionRepository stores in userConnections table?

    1. Right after the user logs in (authenticated and authorized), the UserConnectionRepository creates a record. See the bean declaration at

    2. how I can use MongoDB instead of mysq?. i faced problems in configuration so what are the changes that i have to make? thank you for help

  3. Mark,

    I'm having trouble instantiating connect controller. At the time it is created, there is no user logged in (app just starts). So, everytime it fails during connectionRepository() due to "no user signed in".

    Please help!


  4. I face exactly the same problem as Ryan.. its fails during connectionRepository() due to "no user signed in".


  5. how I can use MongoDB instead of mysq?. i faced problems in configuration so what are the changes that i have to make? thank you for help

  6. generate OAuth keys for Facebook and Twitter (Online Java Training) Generate OAuth keys Java Training in Chennai Consumer key and Consumer secret values: J2EE Training in Chennai

  7. Thanks for all your information.Website is very nice and informative content.

    MongoDB Training in Chennai

  8. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai


  9. What you do is beautiful and very sexy. Please take care and do not shorten what you do
    العاب سباق 2018
    العاب طبخ
    العاب تلبيس

  10. Do you need free Google+ Circles?
    Did you know you can get them ON AUTO-PILOT AND ABSOLUTELY FREE by getting an account on Like 4 Like?

  11. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    core java training in Electronic City

    Hibernate Training in electronic city

    spring training in electronic city

    java j2ee training in electronic city

  12. I am grateful to the owner of this site which really shares this wonderful work of this site.That is actually great and useful information.I'm satisfied with just sharing this useful information with us. Please keep it up to date like this.Thank you for sharing..
    website designing company in patna