Quick Start

kdonald edited this page Jun 24, 2011 · 63 revisions

The following steps will get you up and running with Spring Social in your web application. It assumes that you're familiar with Spring Dependency Injection concepts, Spring MVC, and Spring Java Configuration. It also assumes that you will be adding Spring Social to a Spring 3.1 application.

1. Add Spring Social Dependencies

Before you can use Spring Social, you must add the library and its required dependencies to your project.

Core and Web Modules

First, add the core and web modules to your project:



Provider Modules

Next, add the provider modules for the providers you wish to integrate into your application. Facebook is shown as an example below:


Spring Security Crypto Module (Optional)

If you're not already using Spring Security 3.1 to secure your application, you'll need to add the standalone crypto module. This is required for OAuth1 request signing and encrypting credentials when persisting Connection data. If you're already using Spring Security 3.1, there is nothing for you to do because the crypto library comes included.


2. Configure Spring Social

Create a @Configuration class:

 * Spring Social Configuration.
public class SocialConfig {


Add a ConnectionFactoryLocator that has registered the providers your app connects to:

public ConnectionFactoryLocator connectionFactoryLocator() {
    ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
    registry.addConnectionFactory(new FacebookConnectionFactory(environment.getProperty("facebook.clientId"),
    return registry;

private Environment environment;

Add a UsersConnectionRepository for persisting Connection data across all users:

public UsersConnectionRepository usersConnectionRepository() {
    JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource, 
        connectionFactoryLocator(), Encryptors.noOpText());
    repository.setConnectionSignUp(new SimpleConnectionSignUp());
    return repository;

private DataSource dataSource;

Add a ConnectionRepository for managing the current user's connections:

@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {
    User user = SecurityContext.getCurrentUser();
    return usersConnectionRepository().createConnectionRepository(user.getId());

Add one or more request-scoped beans representing current user API bindings. Facebook is shown here:

@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)	
public Facebook facebook() {
    return connectionRepository().getPrimaryConnection(Facebook.class).getApi();

Add a ProviderSignInController that allows users to sign-in using their provider accounts:

public ProviderSignInController providerSignInController() {
    return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(),
        new SimpleSignInAdapter());

3. Create Views

Create a "signin" view that allows users to sign-in with their provider account:

<%@ page session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<form action="<c:url value="/signin/facebook" />" method="POST">
    <button type="submit">Sign in with Facebook</button>

4. Invoke APIs

Finally, @Inject references to API bindings into the objects that need them. An example @Controller that retrieves the current user's facebook friends is shown below:

public class HomeController {

    private final Facebook facebook;

    public HomeController(Facebook facebook) {
        this.facebook = facebook;
    @RequestMapping(value="/", method=RequestMethod.GET)
    public String home(Model model) {
        List<Reference> friends = facebook.friendOperations().getFriends();
        model.addAttribute("friends", friends);
        return "home";

5. Enjoy using Spring Social!

A executable version of this quickstart is available as a sample application. It includes the full SocialConfig source code you may plug into your own application. A Spring Framework 3.0.5 compatible version is also available.

Refer to the reference documentation, additional samples, and Greenhouse for more information on using Spring Social.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.