7cc9a63b5ce2237cdffa553dab35a3a67b9563ba
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SocialConfig.java
1 package de.juplo.yourshouter;
2
3
4
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;
30
31
32 /**
33  * Spring Social Configuration.
34  *
35  * @author Kai Moritz
36  */
37 @Configuration
38 @EnableSocial
39 public class SocialConfig extends SocialConfigurerAdapter
40 {
41   @Inject
42   ConnectionSignUp connectionSignUp;
43   @Inject
44   SignInAdapter signInAdapter;
45
46
47   /**
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>.
51    *
52    * @param config
53    * @param env 
54    */
55   @Override
56   public void addConnectionFactories(
57       ConnectionFactoryConfigurer config,
58       Environment env
59       )
60   {
61     config.addConnectionFactory(
62         new FacebookConnectionFactory(
63             env.getProperty("facebook.app.id"),
64             env.getProperty("facebook.app.secret")
65             )
66         );
67   }
68
69   /**
70    * Configure an instance of {@link InMemoryUsersConnection} as persistent
71    * store of user/connection-mappings.
72    *
73    * At the moment, no special configuration is needed.
74    *
75    * @param connectionFactoryLocator
76    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
77    * @return
78    *     The configured {@link UsersConnectionRepository}.
79    */
80   @Override
81   public UsersConnectionRepository getUsersConnectionRepository(
82       ConnectionFactoryLocator connectionFactoryLocator
83       )
84   {
85     InMemoryUsersConnectionRepository repository =
86         new InMemoryUsersConnectionRepository(connectionFactoryLocator);
87     repository.setConnectionSignUp(connectionSignUp);
88     return repository;
89   }
90
91   /**
92    * Configure our new implementation of {@link UserIdSource}, that retrieves
93    * the current user from the {@link SecurityContext}.
94    *
95    * @return
96    *     An instance of {@link AnonymousUserIdSource}.
97    *
98    * @see {@link SecurityContextUserIdSource}
99    * @see {@link SecurityContext}
100    * @see {@link UserCookieInterceptor}
101    */
102   @Override
103   public UserIdSource getUserIdSource()
104   {
105     return new SpringSecurityContextUserIdSource();
106   }
107
108
109   /**
110    * Configuration of the controller, that handles the authorization against
111    * the Facebook-API, to connect a user to Facebook.
112    *
113    * At the moment, no special configuration is needed.
114    *
115    * @param factoryLocator
116    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
117    * @param repository
118    *     The {@link ConnectionRepository} will be injected by Spring.
119    * @return
120    *     The configured controller.
121    */
122   @Bean
123   public ConnectController connectController(
124       ConnectionFactoryLocator factoryLocator,
125       ConnectionRepository repository
126       )
127   {
128     ConnectController controller =
129         new ConnectController(factoryLocator, repository);
130     return controller;
131   }
132
133   /**
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.
137    *
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}
141    */
142   @Bean
143   public ProviderSignInController signInController(
144       ConnectionFactoryLocator factoryLocator,
145       UsersConnectionRepository repository
146       )
147   {
148     ProviderSignInController controller =
149         new ProviderSignInController(factoryLocator, repository, signInAdapter);
150     return controller;
151   }
152
153   /**
154    * Configure the {@link CanvasSignInController} to enable sign-in through
155    * the <code>signed_request</code>, that Facebook sends to the canvas-page.
156    *
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}
161    */
162   @Bean
163   public CanvasSignInController canvasSignInController(
164       ConnectionFactoryLocator factoryLocator,
165       UsersConnectionRepository repository,
166       Environment env
167       )
168   {
169     return
170         new CanvasSignInController(
171             factoryLocator,
172             repository,
173             signInAdapter,
174             env.getProperty("facebook.app.id"),
175             env.getProperty("facebook.app.secret"),
176             env.getProperty("facebook.app.canvas")
177             );
178   }
179
180   /**
181    * Configure a scoped bean named <code>facebook</code>, that enables
182    * access to the Graph-API in the name of the current user.
183    *
184    * @param repository
185    *     The {@link ConnectionRepository} will be injected by Spring.
186    * @return
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.
190    */
191   @Bean
192   @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
193   public Facebook facebook(ConnectionRepository repository)
194   {
195     Connection<Facebook> connection =
196         repository.findPrimaryConnection(Facebook.class);
197     return connection != null ? connection.getApi() : null;
198   }
199
200   /**
201    * Use the <code>HttpClient</code> from Apaches <code>HttpComponents</code>
202    * for HTTP-requests.
203    *
204    * We also configure shorter intervals for the connection timeout and the
205    * read timeout.
206    *
207    * @param env The {@link Environment}, to read additional parameters from.
208    * @return The alternative implementation of {@link HttpRequestFactory}.
209    */
210   @Bean
211   public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
212   {
213     HttpComponentsClientHttpRequestFactory factory =
214         new HttpComponentsClientHttpRequestFactory();
215     factory.setConnectTimeout(
216         Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
217         );
218     factory.setReadTimeout(
219         Integer.parseInt(env.getProperty("httpclient.timeout.read"))
220         );
221     return factory;
222   }
223 }