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.connect.web.ProviderSignInController;
21 import org.springframework.social.connect.web.SignInAdapter;
22 import org.springframework.social.facebook.api.Facebook;
23 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
27 * Spring Social Configuration.
33 public class SocialConfig extends SocialConfigurerAdapter
36 * Add a {@link FacebookConnectionFactory} to the configuration.
37 * The factory is configured through the keys <code>facebook.app.id</code>
38 * and <code>facebook.app.secret</code>.
44 public void addConnectionFactories(
45 ConnectionFactoryConfigurer config,
49 config.addConnectionFactory(
50 new FacebookConnectionFactory(
51 env.getProperty("facebook.app.id"),
52 env.getProperty("facebook.app.secret")
58 * Configure an instance of {@link InMemoryUsersConnection} as persistent
59 * store of user/connection-mappings.
61 * At the moment, no special configuration is needed.
63 * @param connectionFactoryLocator
64 * The {@link ConnectionFactoryLocator} will be injected by Spring.
66 * The configured {@link UsersConnectionRepository}.
69 public UsersConnectionRepository getUsersConnectionRepository(
70 ConnectionFactoryLocator connectionFactoryLocator
73 InMemoryUsersConnectionRepository repository =
74 new InMemoryUsersConnectionRepository(connectionFactoryLocator);
75 repository.setConnectionSignUp(new ProviderUserIdConnectionSignUp());
80 * Configure our new implementation of {@link UserIdSource}, that retrieves
81 * the current user from the {@link SecurityContext}.
84 * An instance of {@link AnonymousUserIdSource}.
86 * @see {@link SecurityContextUserIdSource}
87 * @see {@link SecurityContext}
88 * @see {@link UserCookieInterceptor}
91 public UserIdSource getUserIdSource()
93 return new SecurityContextUserIdSource();
98 * Configuration of the controller, that handles the authorization against
99 * the Facebook-API, to connect a user to Facebook.
101 * At the moment, no special configuration is needed.
103 * @param factoryLocator
104 * The {@link ConnectionFactoryLocator} will be injected by Spring.
106 * The {@link ConnectionRepository} will be injected by Spring.
108 * The configured controller.
111 public ConnectController connectController(
112 ConnectionFactoryLocator factoryLocator,
113 ConnectionRepository repository
116 ConnectController controller =
117 new ConnectController(factoryLocator, repository);
122 * Configure the {@link ProviderSignInController} to use our implementation
123 * of {@link SignInAdapter} to sign in the user by storing the ID in the
124 * {@link SecurityContext} and the user-cookie.
126 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
127 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
128 * @return The configured {@link ProviderSignInController}
131 public ProviderSignInController signInController(
132 ConnectionFactoryLocator factoryLocator,
133 UsersConnectionRepository repository
136 ProviderSignInController controller = new ProviderSignInController(
139 new UserCookieSignInAdapter()
145 * Configure a scoped bean named <code>facebook</code>, that enables
146 * access to the Graph-API in the name of the current user.
149 * The {@link ConnectionRepository} will be injected by Spring.
151 * A {@Connection<Facebook>}, that represents the authorization of the
152 * current user against the Graph-API, or <code>null</code>, if the
153 * current user is not connected to the API.
156 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
157 public Facebook facebook(ConnectionRepository repository)
159 Connection<Facebook> connection =
160 repository.findPrimaryConnection(Facebook.class);
161 return connection != null ? connection.getApi() : null;