1 package de.juplo.yourshouter;
5 import org.springframework.context.annotation.Bean;
6 import org.springframework.context.annotation.Configuration;
7 import org.springframework.context.annotation.Scope;
8 import org.springframework.context.annotation.ScopedProxyMode;
9 import org.springframework.social.UserIdSource;
10 import org.springframework.core.env.Environment;
11 import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
12 import org.springframework.social.config.annotation.EnableSocial;
13 import org.springframework.social.config.annotation.SocialConfigurerAdapter;
14 import org.springframework.social.connect.Connection;
15 import org.springframework.social.connect.ConnectionFactoryLocator;
16 import org.springframework.social.connect.ConnectionRepository;
17 import org.springframework.social.connect.UsersConnectionRepository;
18 import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
19 import org.springframework.social.connect.web.ConnectController;
20 import org.springframework.social.facebook.api.Facebook;
21 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
25 * Spring Social Configuration.
31 public class SocialConfig extends SocialConfigurerAdapter
34 * Add a {@link FacebookConnectionFactory} to the configuration.
35 * The factory is configured through the keys <code>facebook.app.id</code>
36 * and <code>facebook.app.secret</code>.
42 public void addConnectionFactories(
43 ConnectionFactoryConfigurer config,
47 config.addConnectionFactory(
48 new FacebookConnectionFactory(
49 env.getProperty("facebook.app.id"),
50 env.getProperty("facebook.app.secret")
56 * Configure an instance of {@link InMemoryUsersConnection} as persistent
57 * store of user/connection-mappings.
59 * At the moment, no special configuration is needed.
61 * @param connectionFactoryLocator
62 * The {@link ConnectionFactoryLocator} will be injected by Spring.
64 * The configured {@link UsersConnectionRepository}.
67 public UsersConnectionRepository getUsersConnectionRepository(
68 ConnectionFactoryLocator connectionFactoryLocator
71 InMemoryUsersConnectionRepository repository =
72 new InMemoryUsersConnectionRepository(connectionFactoryLocator);
73 repository.setConnectionSignUp(new ProviderUserIdConnectionSignUp());
78 * Configure our new implementation of {@link UserIdSource}, that retrieves
79 * the current user from the {@link SecurityContext}.
82 * An instance of {@link AnonymousUserIdSource}.
84 * @see {@link SecurityContextUserIdSource}
85 * @see {@link SecurityContext}
86 * @see {@link UserCookieInterceptor}
89 public UserIdSource getUserIdSource()
91 return new SecurityContextUserIdSource();
96 * Configuration of the controller, that handles the authorization against
97 * the Facebook-API, to connect a user to Facebook.
99 * At the moment, no special configuration is needed.
101 * @param factoryLocator
102 * The {@link ConnectionFactoryLocator} will be injected by Spring.
104 * The {@link ConnectionRepository} will be injected by Spring.
106 * The configured controller.
109 public ConnectController connectController(
110 ConnectionFactoryLocator factoryLocator,
111 ConnectionRepository repository
114 ConnectController controller =
115 new ConnectController(factoryLocator, repository);
120 * Configure a scoped bean named <code>facebook</code>, that enables
121 * access to the Graph-API in the name of the current user.
124 * The {@link ConnectionRepository} will be injected by Spring.
126 * A {@Connection<Facebook>}, that represents the authorization of the
127 * current user against the Graph-API, or <code>null</code>, if the
128 * current user is not connected to the API.
131 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
132 public Facebook facebook(ConnectionRepository repository)
134 Connection<Facebook> connection =
135 repository.findPrimaryConnection(Facebook.class);
136 return connection != null ? connection.getApi() : null;