1 package de.juplo.yourshouter;
5 import javax.inject.Inject;
6 import javax.sql.DataSource;
7 import org.apache.http.HttpRequestFactory;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.context.annotation.Scope;
11 import org.springframework.context.annotation.ScopedProxyMode;
12 import org.springframework.social.UserIdSource;
13 import org.springframework.core.env.Environment;
14 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
15 import org.springframework.security.core.context.SecurityContext;
16 import org.springframework.security.crypto.encrypt.Encryptors;
17 import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
18 import org.springframework.social.config.annotation.EnableSocial;
19 import org.springframework.social.config.annotation.SocialConfigurerAdapter;
20 import org.springframework.social.connect.Connection;
21 import org.springframework.social.connect.ConnectionFactoryLocator;
22 import org.springframework.social.connect.ConnectionRepository;
23 import org.springframework.social.connect.ConnectionSignUp;
24 import org.springframework.social.connect.UsersConnectionRepository;
25 import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
26 import org.springframework.social.connect.web.ConnectController;
27 import org.springframework.social.connect.web.ProviderSignInController;
28 import org.springframework.social.connect.web.SignInAdapter;
29 import org.springframework.social.facebook.api.Facebook;
30 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
31 import org.springframework.social.facebook.web.CanvasSignInController;
35 * Spring Social Configuration.
41 public class SocialConfig extends SocialConfigurerAdapter
44 DataSource dataSource;
46 ConnectionSignUp connectionSignUp;
48 SignInAdapter signInAdapter;
52 * Add a {@link FacebookConnectionFactory} to the configuration.
53 * The factory is configured through the keys <code>facebook.app.id</code>
54 * and <code>facebook.app.secret</code>.
60 public void addConnectionFactories(
61 ConnectionFactoryConfigurer config,
65 config.addConnectionFactory(
66 new FacebookConnectionFactory(
67 env.getProperty("facebook.app.id"),
68 env.getProperty("facebook.app.secret")
76 * Configure an instance of {@link JdbcUsersConnection} as persistent
77 * store of user/connection-mappings.
79 * The app-secret is reused as password for the encryption of the data.
80 * The salt can be changed in the <code>pom.xml</code>
82 * This does only work, if you have the Java Crypto Extension (JCE) in
83 * full strength version, since Spring Security is using a 256-bit key.
85 * @see http://stackoverflow.com/a/17637354
88 public UsersConnectionRepository getUsersConnectionRepository(
89 ConnectionFactoryLocator connectionFactoryLocator
92 JdbcUsersConnectionRepository repository =
93 new JdbcUsersConnectionRepository(
95 connectionFactoryLocator,
98 repository.setConnectionSignUp(connectionSignUp);
103 * Configure our new implementation of {@link UserIdSource}, that retrieves
104 * the current user from the {@link SecurityContext}.
107 * An instance of {@link AnonymousUserIdSource}.
109 * @see {@link SecurityContextUserIdSource}
110 * @see {@link SecurityContext}
111 * @see {@link UserCookieInterceptor}
114 public UserIdSource getUserIdSource()
116 return new SpringSecurityContextUserIdSource();
121 * Configuration of the controller, that handles the authorization against
122 * the Facebook-API, to connect a user to Facebook.
124 * At the moment, no special configuration is needed.
126 * @param factoryLocator
127 * The {@link ConnectionFactoryLocator} will be injected by Spring.
129 * The {@link ConnectionRepository} will be injected by Spring.
131 * The configured controller.
134 public ConnectController connectController(
135 ConnectionFactoryLocator factoryLocator,
136 ConnectionRepository repository
139 ConnectController controller =
140 new ConnectController(factoryLocator, repository);
145 * Configure the {@link ProviderSignInController} to use our implementation
146 * of {@link SignInAdapter} to sign in the user by storing the ID in the
147 * {@link SecurityContext} and the user-cookie.
149 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
150 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
151 * @return The configured {@link ProviderSignInController}
154 public ProviderSignInController signInController(
155 ConnectionFactoryLocator factoryLocator,
156 UsersConnectionRepository repository
159 ProviderSignInController controller =
160 new ProviderSignInController(factoryLocator, repository, signInAdapter);
165 * Configure the {@link CanvasSignInController} to enable sign-in through
166 * the <code>signed_request</code>, that Facebook sends to the canvas-page.
168 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
169 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
170 * @param env The {@link Environment}, to read additional parameters from.
171 * @return The configured {@link CanvasSignInController}
174 public CanvasSignInController canvasSignInController(
175 ConnectionFactoryLocator factoryLocator,
176 UsersConnectionRepository repository,
181 new CanvasSignInController(
185 env.getProperty("facebook.app.id"),
186 env.getProperty("facebook.app.secret"),
187 env.getProperty("facebook.app.canvas")
192 * Configure a scoped bean named <code>facebook</code>, that enables
193 * access to the Graph-API in the name of the current user.
196 * The {@link ConnectionRepository} will be injected by Spring.
198 * A {@Connection<Facebook>}, that represents the authorization of the
199 * current user against the Graph-API, or <code>null</code>, if the
200 * current user is not connected to the API.
203 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
204 public Facebook facebook(ConnectionRepository repository)
206 Connection<Facebook> connection =
207 repository.findPrimaryConnection(Facebook.class);
208 return connection != null ? connection.getApi() : null;
212 * Use the <code>HttpClient</code> from Apaches <code>HttpComponents</code>
215 * We also configure shorter intervals for the connection timeout and the
218 * @param env The {@link Environment}, to read additional parameters from.
219 * @return The alternative implementation of {@link HttpRequestFactory}.
222 public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
224 HttpComponentsClientHttpRequestFactory factory =
225 new HttpComponentsClientHttpRequestFactory();
226 factory.setConnectTimeout(
227 Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
229 factory.setReadTimeout(
230 Integer.parseInt(env.getProperty("httpclient.timeout.read"))