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.social.config.annotation.ConnectionFactoryConfigurer;
15 import org.springframework.social.config.annotation.EnableSocial;
16 import org.springframework.social.config.annotation.SocialConfigurerAdapter;
17 import org.springframework.social.connect.Connection;
18 import org.springframework.social.connect.ConnectionFactoryLocator;
19 import org.springframework.social.connect.ConnectionRepository;
20 import org.springframework.social.connect.ConnectionSignUp;
21 import org.springframework.social.connect.UsersConnectionRepository;
22 import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
23 import org.springframework.social.connect.web.ConnectController;
24 import org.springframework.social.connect.web.ProviderSignInController;
25 import org.springframework.social.connect.web.SignInAdapter;
26 import org.springframework.social.facebook.api.Facebook;
27 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
28 import org.springframework.social.facebook.web.CanvasSignInController;
32 * Spring Social Configuration.
38 public class SocialConfig extends SocialConfigurerAdapter
41 ConnectionSignUp connectionSignUp;
43 SignInAdapter signInAdapter;
47 * Add a {@link FacebookConnectionFactory} to the configuration.
48 * The factory is configured through the keys <code>facebook.app.id</code>
49 * and <code>facebook.app.secret</code>.
55 public void addConnectionFactories(
56 ConnectionFactoryConfigurer config,
60 config.addConnectionFactory(
61 new FacebookConnectionFactory(
62 env.getProperty("facebook.app.id"),
63 env.getProperty("facebook.app.secret")
69 * Configure an instance of {@link InMemoryUsersConnection} as persistent
70 * store of user/connection-mappings.
72 * At the moment, no special configuration is needed.
74 * @param connectionFactoryLocator
75 * The {@link ConnectionFactoryLocator} will be injected by Spring.
77 * The configured {@link UsersConnectionRepository}.
80 public UsersConnectionRepository getUsersConnectionRepository(
81 ConnectionFactoryLocator connectionFactoryLocator
84 InMemoryUsersConnectionRepository repository =
85 new InMemoryUsersConnectionRepository(connectionFactoryLocator);
86 repository.setConnectionSignUp(connectionSignUp);
91 * Configure our new implementation of {@link UserIdSource}, that retrieves
92 * the current user from the {@link SecurityContext}.
95 * An instance of {@link AnonymousUserIdSource}.
97 * @see {@link SecurityContextUserIdSource}
98 * @see {@link SecurityContext}
99 * @see {@link UserCookieInterceptor}
102 public UserIdSource getUserIdSource()
104 return new SecurityContextUserIdSource();
109 * Configuration of the controller, that handles the authorization against
110 * the Facebook-API, to connect a user to Facebook.
112 * At the moment, no special configuration is needed.
114 * @param factoryLocator
115 * The {@link ConnectionFactoryLocator} will be injected by Spring.
117 * The {@link ConnectionRepository} will be injected by Spring.
119 * The configured controller.
122 public ConnectController connectController(
123 ConnectionFactoryLocator factoryLocator,
124 ConnectionRepository repository
127 ConnectController controller =
128 new ConnectController(factoryLocator, repository);
133 * Configure the {@link ProviderSignInController} to use our implementation
134 * of {@link SignInAdapter} to sign in the user by storing the ID in the
135 * {@link SecurityContext} and the user-cookie.
137 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
138 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
139 * @return The configured {@link ProviderSignInController}
142 public ProviderSignInController signInController(
143 ConnectionFactoryLocator factoryLocator,
144 UsersConnectionRepository repository
147 ProviderSignInController controller =
148 new ProviderSignInController(factoryLocator, repository, signInAdapter);
153 * Configure the {@link CanvasSignInController} to enable sign-in through
154 * the <code>signed_request</code>, that Facebook sends to the canvas-page.
156 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
157 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
158 * @param env The {@link Environment}, to read additional parameters from.
159 * @return The configured {@link CanvasSignInController}
162 public CanvasSignInController canvasSignInController(
163 ConnectionFactoryLocator factoryLocator,
164 UsersConnectionRepository repository,
169 new CanvasSignInController(
173 env.getProperty("facebook.app.id"),
174 env.getProperty("facebook.app.secret"),
175 env.getProperty("facebook.app.canvas")
180 * Configure a scoped bean named <code>facebook</code>, that enables
181 * access to the Graph-API in the name of the current user.
184 * The {@link ConnectionRepository} will be injected by Spring.
186 * A {@Connection<Facebook>}, that represents the authorization of the
187 * current user against the Graph-API, or <code>null</code>, if the
188 * current user is not connected to the API.
191 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
192 public Facebook facebook(ConnectionRepository repository)
194 Connection<Facebook> connection =
195 repository.findPrimaryConnection(Facebook.class);
196 return connection != null ? connection.getApi() : null;
200 * Use the <code>HttpClient</code> from Apaches <code>HttpComponents</code>
203 * We also configure shorter intervals for the connection timeout and the
206 * @param env The {@link Environment}, to read additional parameters from.
207 * @return The alternative implementation of {@link HttpRequestFactory}.
210 public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
212 HttpComponentsClientHttpRequestFactory factory =
213 new HttpComponentsClientHttpRequestFactory();
214 factory.setConnectTimeout(
215 Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
217 factory.setReadTimeout(
218 Integer.parseInt(env.getProperty("httpclient.timeout.read"))