X-Git-Url: https://juplo.de/gitweb/?p=examples%2Ffacebook-app;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FSocialConfig.java;h=4efa0e2e592911f09a152814984126c631969485;hp=2abcd4283f3db0844c2bd5755357c53ba318066c;hb=f45e5e611f7d990477ede18dfc79a18d926eb347;hpb=02f599692669d48f9865764fda994ad61d203ffb diff --git a/src/main/java/de/juplo/yourshouter/SocialConfig.java b/src/main/java/de/juplo/yourshouter/SocialConfig.java index 2abcd42..4efa0e2 100644 --- a/src/main/java/de/juplo/yourshouter/SocialConfig.java +++ b/src/main/java/de/juplo/yourshouter/SocialConfig.java @@ -2,12 +2,14 @@ package de.juplo.yourshouter; +import org.apache.http.HttpRequestFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.social.UserIdSource; import org.springframework.core.env.Environment; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.social.config.annotation.ConnectionFactoryConfigurer; import org.springframework.social.config.annotation.EnableSocial; import org.springframework.social.config.annotation.SocialConfigurerAdapter; @@ -17,8 +19,11 @@ import org.springframework.social.connect.ConnectionRepository; import org.springframework.social.connect.UsersConnectionRepository; import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository; import org.springframework.social.connect.web.ConnectController; +import org.springframework.social.connect.web.ProviderSignInController; +import org.springframework.social.connect.web.SignInAdapter; import org.springframework.social.facebook.api.Facebook; import org.springframework.social.facebook.connect.FacebookConnectionFactory; +import org.springframework.social.facebook.web.CanvasSignInController; /** @@ -116,6 +121,56 @@ public class SocialConfig extends SocialConfigurerAdapter return controller; } + /** + * Configure the {@link ProviderSignInController} to use our implementation + * of {@link SignInAdapter} to sign in the user by storing the ID in the + * {@link SecurityContext} and the user-cookie. + * + * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring. + * @param repository The {@link UserConnectionRepository} will be injected by Spring. + * @return The configured {@link ProviderSignInController} + */ + @Bean + public ProviderSignInController signInController( + ConnectionFactoryLocator factoryLocator, + UsersConnectionRepository repository + ) + { + ProviderSignInController controller = new ProviderSignInController( + factoryLocator, + repository, + new UserCookieSignInAdapter() + ); + return controller; + } + + /** + * Configure the {@link CanvasSignInController} to enable sign-in through + * the signed_request, that Facebook sends to the canvas-page. + * + * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring. + * @param repository The {@link UserConnectionRepository} will be injected by Spring. + * @param env The {@link Environment}, to read additional parameters from. + * @return The configured {@link CanvasSignInController} + */ + @Bean + public CanvasSignInController canvasSignInController( + ConnectionFactoryLocator factoryLocator, + UsersConnectionRepository repository, + Environment env + ) + { + return + new CanvasSignInController( + factoryLocator, + repository, + new UserCookieSignInAdapter(), + env.getProperty("facebook.app.id"), + env.getProperty("facebook.app.secret"), + env.getProperty("facebook.app.canvas") + ); + } + /** * Configure a scoped bean named facebook, that enables * access to the Graph-API in the name of the current user. @@ -135,4 +190,28 @@ public class SocialConfig extends SocialConfigurerAdapter repository.findPrimaryConnection(Facebook.class); return connection != null ? connection.getApi() : null; } + + /** + * Use the HttpClient from Apaches HttpComponents + * for HTTP-requests. + * + * We also configure shorter intervals for the connection timeout and the + * read timeout. + * + * @param env The {@link Environment}, to read additional parameters from. + * @return The alternative implementation of {@link HttpRequestFactory}. + */ + @Bean + public HttpComponentsClientHttpRequestFactory requestFactory(Environment env) + { + HttpComponentsClientHttpRequestFactory factory = + new HttpComponentsClientHttpRequestFactory(); + factory.setConnectTimeout( + Integer.parseInt(env.getProperty("httpclient.timeout.connection")) + ); + factory.setReadTimeout( + Integer.parseInt(env.getProperty("httpclient.timeout.read")) + ); + return factory; + } }