Sign in users via Facebook and sign up new users automatically
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SocialConfig.java
index 5308709..ff69151 100644 (file)
@@ -17,6 +17,8 @@ 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;
 
@@ -70,22 +72,25 @@ public class SocialConfig extends SocialConfigurerAdapter
   {
     InMemoryUsersConnectionRepository repository =
         new InMemoryUsersConnectionRepository(connectionFactoryLocator);
+    repository.setConnectionSignUp(new ProviderUserIdConnectionSignUp());
     return repository;
   }
 
   /**
-   * Configure a {@link UserIdSource}, that is equivalent to the one, that is
-   * created by Spring-Boot.
+   * Configure our new implementation of {@link UserIdSource}, that retrieves
+   * the current user from the {@link SecurityContext}.
    *
    * @return
    *     An instance of {@link AnonymousUserIdSource}.
    *
-   * @see {@link AnonymousUserIdSource}
+   * @see {@link SecurityContextUserIdSource}
+   * @see {@link SecurityContext}
+   * @see {@link UserCookieInterceptor}
    */
   @Override
   public UserIdSource getUserIdSource()
   {
-    return new AnonymousUserIdSource();
+    return new SecurityContextUserIdSource();
   }
 
 
@@ -113,6 +118,29 @@ 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 a scoped bean named <code>facebook</code>, that enables
    * access to the Graph-API in the name of the current user.