Monday, December 10, 2012

Spring Social with JavaConfig (Part 3)

Review

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
    • ApplicationInitializer.java
    • ApplicationContext.java
    • DataConfig.java
    • ThymeleafConfig.java
    • spring.properties
  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.

Features:
  • 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.
Source: http://www.springsource.org/spring-social

Here's our Spring Social configuration:

SocialConfig.java


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

Next

In the next section, we will focus on Spring Security-related configuration. Click here to proceed.
StumpleUpon DiggIt! Del.icio.us 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

13 comments:

  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.

    ReplyDelete
  2. how does the UsersConnectionRepository stores in userConnections table?

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

      Delete
    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

      Delete
  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!

    -Ryan

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

    cheers
    Joerg

    ReplyDelete
  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

    ReplyDelete
  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

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

    MongoDB Training in Chennai

    ReplyDelete
  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

    ReplyDelete
  9. 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

    ReplyDelete
  10. Let me tell you something...

    What I'm going to tell you may sound kind of weird, and maybe even a little "out there..."

    BUT what if you could just click "PLAY" to LISTEN to a short, "musical tone"...

    And magically attract MORE MONEY to your life??

    What I'm talking about is BIG MONEY, even MILLIONS of DOLLARS!!

    Do you think it's too EASY?? Think it's IMPOSSIBLE?!?

    Well, I've got news for you.

    Usually the greatest blessings in life are the SIMPLEST!!

    Honestly, I will provide you with PROOF by allowing you to listen to a real-life "magical money tone" I've produced...

    You just hit "PLAY" and you will start having more money come into your life... starting almost INSTANTLY...

    GO here now to play this marvelous "Miracle Money-Magnet Tone" - as my gift to you!!

    ReplyDelete