1 package de.juplo.yourshouter;
5 import javax.inject.Inject;
6 import org.apache.http.HttpRequestFactory;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.context.annotation.Configuration;
9 import org.springframework.context.annotation.Scope;
10 import org.springframework.context.annotation.ScopedProxyMode;
11 import org.springframework.social.UserIdSource;
12 import org.springframework.core.env.Environment;
13 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
14 import org.springframework.security.core.context.SecurityContext;
15 import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
16 import org.springframework.social.config.annotation.EnableSocial;
17 import org.springframework.social.config.annotation.SocialConfigurerAdapter;
18 import org.springframework.social.connect.Connection;
19 import org.springframework.social.connect.ConnectionFactoryLocator;
20 import org.springframework.social.connect.ConnectionRepository;
21 import org.springframework.social.connect.ConnectionSignUp;
22 import org.springframework.social.connect.UsersConnectionRepository;
23 import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
24 import org.springframework.social.connect.web.ConnectController;
25 import org.springframework.social.connect.web.ProviderSignInController;
26 import org.springframework.social.connect.web.SignInAdapter;
27 import org.springframework.social.facebook.api.Facebook;
28 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
29 import org.springframework.social.facebook.web.CanvasSignInController;
33 * Spring Social Configuration.
39 public class SocialConfig extends SocialConfigurerAdapter
42 ConnectionSignUp connectionSignUp;
44 SignInAdapter signInAdapter;
48 * Add a {@link FacebookConnectionFactory} to the configuration.
49 * The factory is configured through the keys <code>facebook.app.id</code>
50 * and <code>facebook.app.secret</code>.
56 public void addConnectionFactories(
57 ConnectionFactoryConfigurer config,
61 config.addConnectionFactory(
62 new FacebookConnectionFactory(
63 env.getProperty("facebook.app.id"),
64 env.getProperty("facebook.app.secret")
70 * Configure an instance of {@link InMemoryUsersConnection} as persistent
71 * store of user/connection-mappings.
73 * At the moment, no special configuration is needed.
75 * @param connectionFactoryLocator
76 * The {@link ConnectionFactoryLocator} will be injected by Spring.
78 * The configured {@link UsersConnectionRepository}.
81 public UsersConnectionRepository getUsersConnectionRepository(
82 ConnectionFactoryLocator connectionFactoryLocator
85 InMemoryUsersConnectionRepository repository =
86 new InMemoryUsersConnectionRepository(connectionFactoryLocator);
87 repository.setConnectionSignUp(connectionSignUp);
92 * Configure our new implementation of {@link UserIdSource}, that retrieves
93 * the current user from the {@link SecurityContext}.
96 * An instance of {@link AnonymousUserIdSource}.
98 * @see {@link SecurityContextUserIdSource}
99 * @see {@link SecurityContext}
100 * @see {@link UserCookieInterceptor}
103 public UserIdSource getUserIdSource()
105 return new SpringSecurityContextUserIdSource();
110 * Configuration of the controller, that handles the authorization against
111 * the Facebook-API, to connect a user to Facebook.
113 * At the moment, no special configuration is needed.
115 * @param factoryLocator
116 * The {@link ConnectionFactoryLocator} will be injected by Spring.
118 * The {@link ConnectionRepository} will be injected by Spring.
120 * The configured controller.
123 public ConnectController connectController(
124 ConnectionFactoryLocator factoryLocator,
125 ConnectionRepository repository
128 ConnectController controller =
129 new ConnectController(factoryLocator, repository);
134 * Configure the {@link ProviderSignInController} to use our implementation
135 * of {@link SignInAdapter} to sign in the user by storing the ID in the
136 * {@link SecurityContext} and the user-cookie.
138 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
139 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
140 * @return The configured {@link ProviderSignInController}
143 public ProviderSignInController signInController(
144 ConnectionFactoryLocator factoryLocator,
145 UsersConnectionRepository repository
148 ProviderSignInController controller =
149 new ProviderSignInController(factoryLocator, repository, signInAdapter);
154 * Configure the {@link CanvasSignInController} to enable sign-in through
155 * the <code>signed_request</code>, that Facebook sends to the canvas-page.
157 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
158 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
159 * @param env The {@link Environment}, to read additional parameters from.
160 * @return The configured {@link CanvasSignInController}
163 public CanvasSignInController canvasSignInController(
164 ConnectionFactoryLocator factoryLocator,
165 UsersConnectionRepository repository,
170 new CanvasSignInController(
174 env.getProperty("facebook.app.id"),
175 env.getProperty("facebook.app.secret"),
176 env.getProperty("facebook.app.canvas")
181 * Configure a scoped bean named <code>facebook</code>, that enables
182 * access to the Graph-API in the name of the current user.
185 * The {@link ConnectionRepository} will be injected by Spring.
187 * A {@Connection<Facebook>}, that represents the authorization of the
188 * current user against the Graph-API, or <code>null</code>, if the
189 * current user is not connected to the API.
192 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
193 public Facebook facebook(ConnectionRepository repository)
195 Connection<Facebook> connection =
196 repository.findPrimaryConnection(Facebook.class);
197 return connection != null ? connection.getApi() : null;
201 * Use the <code>HttpClient</code> from Apaches <code>HttpComponents</code>
204 * We also configure shorter intervals for the connection timeout and the
207 * @param env The {@link Environment}, to read additional parameters from.
208 * @return The alternative implementation of {@link HttpRequestFactory}.
211 public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
213 HttpComponentsClientHttpRequestFactory factory =
214 new HttpComponentsClientHttpRequestFactory();
215 factory.setConnectTimeout(
216 Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
218 factory.setReadTimeout(
219 Integer.parseInt(env.getProperty("httpclient.timeout.read"))