Monday, December 10, 2012

Spring Social with JavaConfig (Part 2)

Review

In the previous section, we have discussed the functional requirements of our application. In this section we will study how to generate OAuth keys for Facebook and Twitter. These are required so that Spring Social can communicate with these social media sites.

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

Generate OAuth keys

Facebook

To generate a Facebook secret key, you need to sign-up for a Facebook account first. Once you have an account, follow these steps:
  1. Open a browser
  2. Visit https://developers.facebook.com/apps
  3. Click on Create New App
  4. Fill-in the App Name
  5. You will be redirected to the Basic settings page
  6. Now copy the App ID value. This is your client ID
  7. Then copy the App Secret value. This is your client secret

Note: The values need to be stored in the spring.properties file (see Part 5).

On my sample app, here's the Basic settings page. I've purposely changed the App ID and App Secret values:


Twitter

To generate a Twitter secret key, you need to sign-up for a Twitter account first. Once you have an account, follow these steps:
  1. Open a browser
  2. Visit https://dev.twitter.com/
  3. Visit the My applications page at https://dev.twitter.com/apps
  4. Click on Create a new application
  5. Fill-in the Name
  6. Fill-in the Description
  7. Fill-in the Website (You will need to invent a fictitious URL)
  8. You will be redirected to the Details tab of your new application
  9. Now copy the Consumer key value. This is your client ID
  10. Then copy the Consumer secret value. This is your client secret

Note: The values need to be stored in the spring.properties file (see Part 5).

On my sample app, here's the Details tab. I've purposely changed the Consumer key and Consumer secret values:



Next

In the next section, we will setup the Spring Social-related configuration through JavaConfig. 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 2) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

10 comments:

  1. Hi krams,
    I've cloned your app from Github repository to my eclipse workspace. When I'm trying to build you app I got an error in pom.xml saying

    "Plugin execution not covered by lifecycle configuration: com.mysema.maven:maven-apt-plugin:1.0:process (execution: default, phase: generate-sources)"

    I can't get that error why it is displaying.

    Can you please suggest me how to build and run this app?


    ReplyDelete
  2. Great tutorial. Maybe add something about where to place the callback url on the Facebook app page? Maybe I missed it. But I appreciate everything youve done. Thank you! I fixed the eclipse error for pom.xml by adding the following.. (if the text comes out wrong then this site might help http://wiki.eclipse.org/M2E_plugin_execution_not_covered)




    org.eclipse.m2e
    lifecycle-mapping
    1.0.0





    com.mysema.maven
    maven-apt-plugin
    [1.0.0,)

    process













    ReplyDelete
  3. Hi sir, i have a problem in connecting facebook so please help me where should i give client ID and client secretid then how to connect facebook


    error Not connected to provider 'facebook'
    Mar 28, 2013 5:14:47 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet spring threw exception
    org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "Connect to Facebook" (facebook/connect:18)

    ReplyDelete
  4. Good tutorial, thanks for putting this together.

    Minor problem in the ThymeleafConfig class line 24:

    resolver.addTemplateAlias("connect/twitterConnected","facebook/connected");

    Should be:

    resolver.addTemplateAlias("connect/twitterConnected","twitter/connected");

    Cheers!

    ReplyDelete
  5. http://2.bp.blogspot.com/-Zdm4t-LBf6U/UJpJEjh7xHI/AAAAAAAABd8/y2RpBfwD7UY/s45/aac46d979989dec1778616a451679a83.png

    ReplyDelete
  6. To generate a Twitter secret key, you need to sign-up for a Twitter account first. Once you have an account, follow these steps:Billig WOW Gold
    World of Warcraft Gold Kaufen Billig

    ReplyDelete
  7. Hello Krams,

    I really need your help on the expression-based access control tutorial. I don't know how to reach you. The question is the following.

    I have seen your posts about expression-based access control. Even though I am new for SpEL, I must use it to implement my spring security project. I want to implement attribute-based access control by using ACL and SpEL. But I failed to go further because of my SpEL limitation. I hope u can help me a little bit how to handle the following problem.

    Consider the following expressions to secure the method 'edit'.

    @PreAuthorize("hasPermission(#data, 'WRITE')and (#user.name == 'David')")
    public Boolean edit(ResourceData data, User user) {
    //do something
    return true;
    }

    Here, what I want to do is securing the method 'edit' by using the two SpELs. the first expression, i.e. hasPermission(#data, 'WRITE' works fine, because I am using ACL for this. But for the 2nd expression I am using database that stores user details of the class 'User'. Assume that I have member variable 'name' for the 'User' class and its corresponding getter and setter methods. I can retrieve 'name' values from DB. But I don't know how to pass those values in this expression, even I don't know how to compare the values in this expression. I am not using XML file for the 2nd expression, I have XML for for the first expression, i.e. for the ACL. By the way this method is quite similar to what you have implemented on the expression-based access control tutorial. The only difference is I want to add extra restrictions by considering user details which is saved from DB.

    Can u provide me some information please? Thanks in advance for your cooperation!

    ReplyDelete
  8. Hi Sir,
    I do follow all your post. its nice and very informative. I have learnt so many things from your post. Thanks you so much Sir for the blogs. Sir, Please keep updating your post. If you have any other blogs or website please do let me know. Thank you in advance.

    ReplyDelete
  9. Hi Sir,
    I do follow all your post. its nice and very informative. I have learnt so many things from your post. Thanks you so much Sir for the blogs. Sir, Please keep updating your post. If you have any other blogs or website please do let me know. Thank you in advance.

    ReplyDelete