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);
77 * Configure a {@link UserIdSource}, that is equivalent to the one, that is
78 * created by Spring-Boot.
81 * An instance of {@link AnonymousUserIdSource}.
83 * @see {@link AnonymousUserIdSource}
86 public UserIdSource getUserIdSource()
88 return new AnonymousUserIdSource();
93 * Configuration of the controller, that handles the authorization against
94 * the Facebook-API, to connect a user to Facebook.
96 * At the moment, no special configuration is needed.
98 * @param factoryLocator
99 * The {@link ConnectionFactoryLocator} will be injected by Spring.
101 * The {@link ConnectionRepository} will be injected by Spring.
103 * The configured controller.
106 public ConnectController connectController(
107 ConnectionFactoryLocator factoryLocator,
108 ConnectionRepository repository
111 ConnectController controller =
112 new ConnectController(factoryLocator, repository);
117 * Configure a scoped bean named <code>facebook</code>, that enables
118 * access to the Graph-API in the name of the current user.
121 * The {@link ConnectionRepository} will be injected by Spring.
123 * A {@Connection<Facebook>}, that represents the authorization of the
124 * current user against the Graph-API, or <code>null</code>, if the
125 * current user is not connected to the API.
128 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
129 public Facebook facebook(ConnectionRepository repository)
131 Connection<Facebook> connection =
132 repository.findPrimaryConnection(Facebook.class);
133 return connection != null ? connection.getApi() : null;