1 package de.juplo.yourshouter;
5 import org.apache.http.HttpRequestFactory;
6 import org.springframework.context.annotation.Bean;
7 import org.springframework.context.annotation.Configuration;
8 import org.springframework.context.annotation.Scope;
9 import org.springframework.context.annotation.ScopedProxyMode;
10 import org.springframework.social.UserIdSource;
11 import org.springframework.core.env.Environment;
12 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
13 import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
14 import org.springframework.social.config.annotation.EnableSocial;
15 import org.springframework.social.config.annotation.SocialConfigurerAdapter;
16 import org.springframework.social.connect.Connection;
17 import org.springframework.social.connect.ConnectionFactoryLocator;
18 import org.springframework.social.connect.ConnectionRepository;
19 import org.springframework.social.connect.UsersConnectionRepository;
20 import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
21 import org.springframework.social.connect.web.ConnectController;
22 import org.springframework.social.connect.web.ProviderSignInController;
23 import org.springframework.social.connect.web.SignInAdapter;
24 import org.springframework.social.facebook.api.Facebook;
25 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
26 import org.springframework.social.facebook.web.CanvasSignInController;
30 * Spring Social Configuration.
36 public class SocialConfig extends SocialConfigurerAdapter
39 * Add a {@link FacebookConnectionFactory} to the configuration.
40 * The factory is configured through the keys <code>facebook.app.id</code>
41 * and <code>facebook.app.secret</code>.
47 public void addConnectionFactories(
48 ConnectionFactoryConfigurer config,
52 config.addConnectionFactory(
53 new FacebookConnectionFactory(
54 env.getProperty("facebook.app.id"),
55 env.getProperty("facebook.app.secret")
61 * Configure an instance of {@link InMemoryUsersConnection} as persistent
62 * store of user/connection-mappings.
64 * At the moment, no special configuration is needed.
66 * @param connectionFactoryLocator
67 * The {@link ConnectionFactoryLocator} will be injected by Spring.
69 * The configured {@link UsersConnectionRepository}.
72 public UsersConnectionRepository getUsersConnectionRepository(
73 ConnectionFactoryLocator connectionFactoryLocator
76 InMemoryUsersConnectionRepository repository =
77 new InMemoryUsersConnectionRepository(connectionFactoryLocator);
78 repository.setConnectionSignUp(new ProviderUserIdConnectionSignUp());
83 * Configure our new implementation of {@link UserIdSource}, that retrieves
84 * the current user from the {@link SecurityContext}.
87 * An instance of {@link AnonymousUserIdSource}.
89 * @see {@link SecurityContextUserIdSource}
90 * @see {@link SecurityContext}
91 * @see {@link UserCookieInterceptor}
94 public UserIdSource getUserIdSource()
96 return new SecurityContextUserIdSource();
101 * Configuration of the controller, that handles the authorization against
102 * the Facebook-API, to connect a user to Facebook.
104 * At the moment, no special configuration is needed.
106 * @param factoryLocator
107 * The {@link ConnectionFactoryLocator} will be injected by Spring.
109 * The {@link ConnectionRepository} will be injected by Spring.
111 * The configured controller.
114 public ConnectController connectController(
115 ConnectionFactoryLocator factoryLocator,
116 ConnectionRepository repository
119 ConnectController controller =
120 new ConnectController(factoryLocator, repository);
125 * Configure the {@link ProviderSignInController} to use our implementation
126 * of {@link SignInAdapter} to sign in the user by storing the ID in the
127 * {@link SecurityContext} and the user-cookie.
129 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
130 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
131 * @return The configured {@link ProviderSignInController}
134 public ProviderSignInController signInController(
135 ConnectionFactoryLocator factoryLocator,
136 UsersConnectionRepository repository
139 ProviderSignInController controller = new ProviderSignInController(
142 new UserCookieSignInAdapter()
148 * Configure the {@link CanvasSignInController} to enable sign-in through
149 * the <code>signed_request</code>, that Facebook sends to the canvas-page.
151 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
152 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
153 * @param env The {@link Environment}, to read additional parameters from.
154 * @return The configured {@link CanvasSignInController}
157 public CanvasSignInController canvasSignInController(
158 ConnectionFactoryLocator factoryLocator,
159 UsersConnectionRepository repository,
164 new CanvasSignInController(
167 new UserCookieSignInAdapter(),
168 env.getProperty("facebook.app.id"),
169 env.getProperty("facebook.app.secret"),
170 env.getProperty("facebook.app.canvas")
175 * Configure a scoped bean named <code>facebook</code>, that enables
176 * access to the Graph-API in the name of the current user.
179 * The {@link ConnectionRepository} will be injected by Spring.
181 * A {@Connection<Facebook>}, that represents the authorization of the
182 * current user against the Graph-API, or <code>null</code>, if the
183 * current user is not connected to the API.
186 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
187 public Facebook facebook(ConnectionRepository repository)
189 Connection<Facebook> connection =
190 repository.findPrimaryConnection(Facebook.class);
191 return connection != null ? connection.getApi() : null;
195 * Use the <code>HttpClient</code> from Apaches <code>HttpComponents</code>
198 * We also configure shorter intervals for the connection timeout and the
201 * @param env The {@link Environment}, to read additional parameters from.
202 * @return The alternative implementation of {@link HttpRequestFactory}.
205 public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
207 HttpComponentsClientHttpRequestFactory factory =
208 new HttpComponentsClientHttpRequestFactory();
209 factory.setConnectTimeout(
210 Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
212 factory.setReadTimeout(
213 Integer.parseInt(env.getProperty("httpclient.timeout.read"))