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.SignInAdapter;
28 import org.springframework.social.facebook.api.Facebook;
29 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
30 import org.springframework.social.facebook.web.CanvasSignInController;
34 * Spring Social Configuration.
40 public class SocialConfig extends SocialConfigurerAdapter
43 DataSource dataSource;
45 ConnectionSignUp connectionSignUp;
47 SignInAdapter signInAdapter;
51 * Add a {@link FacebookConnectionFactory} to the configuration.
52 * The factory is configured through the keys <code>facebook.app.id</code>
53 * and <code>facebook.app.secret</code>.
59 public void addConnectionFactories(
60 ConnectionFactoryConfigurer config,
64 config.addConnectionFactory(
65 new FacebookConnectionFactory(
66 env.getProperty("facebook.app.id"),
67 env.getProperty("facebook.app.secret")
75 * Configure an instance of {@link JdbcUsersConnection} as persistent
76 * store of user/connection-mappings.
78 * The app-secret is reused as password for the encryption of the data.
79 * The salt can be changed in the <code>pom.xml</code>
81 * This does only work, if you have the Java Crypto Extension (JCE) in
82 * full strength version, since Spring Security is using a 256-bit key.
84 * @see http://stackoverflow.com/a/17637354
87 public UsersConnectionRepository getUsersConnectionRepository(
88 ConnectionFactoryLocator connectionFactoryLocator
91 JdbcUsersConnectionRepository repository =
92 new JdbcUsersConnectionRepository(
94 connectionFactoryLocator,
97 repository.setConnectionSignUp(connectionSignUp);
102 * Configure our new implementation of {@link UserIdSource}, that retrieves
103 * the current user from the {@link SecurityContext}.
106 * An instance of {@link AnonymousUserIdSource}.
108 * @see {@link SecurityContextUserIdSource}
109 * @see {@link SecurityContext}
110 * @see {@link UserCookieInterceptor}
113 public UserIdSource getUserIdSource()
115 return new SpringSecurityContextUserIdSource();
120 * Configuration of the controller, that handles the authorization against
121 * the Facebook-API, to connect a user to Facebook.
123 * At the moment, no special configuration is needed.
125 * @param factoryLocator
126 * The {@link ConnectionFactoryLocator} will be injected by Spring.
128 * The {@link ConnectionRepository} will be injected by Spring.
130 * The configured controller.
133 public ConnectController connectController(
134 ConnectionFactoryLocator factoryLocator,
135 ConnectionRepository repository
138 ConnectController controller =
139 new ConnectController(factoryLocator, repository);
144 * Configure the {@link CanvasSignInController} to enable sign-in through
145 * the <code>signed_request</code>, that Facebook sends to the canvas-page.
147 * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
148 * @param repository The {@link UserConnectionRepository} will be injected by Spring.
149 * @param env The {@link Environment}, to read additional parameters from.
150 * @return The configured {@link CanvasSignInController}
153 public CanvasSignInController canvasSignInController(
154 ConnectionFactoryLocator factoryLocator,
155 UsersConnectionRepository repository,
160 new CanvasSignInController(
164 env.getProperty("facebook.app.id"),
165 env.getProperty("facebook.app.secret"),
166 env.getProperty("facebook.app.canvas")
171 * Configure a scoped bean named <code>facebook</code>, that enables
172 * access to the Graph-API in the name of the current user.
175 * The {@link ConnectionRepository} will be injected by Spring.
177 * A {@Connection<Facebook>}, that represents the authorization of the
178 * current user against the Graph-API, or <code>null</code>, if the
179 * current user is not connected to the API.
182 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
183 public Facebook facebook(ConnectionRepository repository)
185 Connection<Facebook> connection =
186 repository.findPrimaryConnection(Facebook.class);
187 return connection != null ? connection.getApi() : null;
191 * Use the <code>HttpClient</code> from Apaches <code>HttpComponents</code>
194 * We also configure shorter intervals for the connection timeout and the
197 * @param env The {@link Environment}, to read additional parameters from.
198 * @return The alternative implementation of {@link HttpRequestFactory}.
201 public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
203 HttpComponentsClientHttpRequestFactory factory =
204 new HttpComponentsClientHttpRequestFactory();
205 factory.setConnectTimeout(
206 Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
208 factory.setReadTimeout(
209 Integer.parseInt(env.getProperty("httpclient.timeout.read"))