Make Spring-Boot's implicit autoconfiguration explicit part-01
authorKai Moritz <kai@juplo.de>
Fri, 22 Jan 2016 09:19:53 +0000 (10:19 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 30 Jan 2016 13:35:11 +0000 (14:35 +0100)
src/main/java/de/juplo/yourshouter/AnonymousUserIdSource.java [new file with mode: 0644]
src/main/java/de/juplo/yourshouter/SocialConfig.java [new file with mode: 0644]
src/main/resources/application.properties

diff --git a/src/main/java/de/juplo/yourshouter/AnonymousUserIdSource.java b/src/main/java/de/juplo/yourshouter/AnonymousUserIdSource.java
new file mode 100644 (file)
index 0000000..c09f400
--- /dev/null
@@ -0,0 +1,21 @@
+package de.juplo.yourshouter;
+
+import org.springframework.social.UserIdSource;
+
+
+/**
+ * Simple implementation of {@link UserIdSource}, that always returns the
+ * string <code>anonymous</code> as user-ID, like the UserIdSource, that is
+ * automatically configured by Spring-Boot, if Spring-Security is not
+ * present.
+ *
+ * @author Kai Moritz
+ */
+public class AnonymousUserIdSource implements UserIdSource
+{
+  @Override
+  public String getUserId()
+  {
+    return "anonymous";
+  }
+}
diff --git a/src/main/java/de/juplo/yourshouter/SocialConfig.java b/src/main/java/de/juplo/yourshouter/SocialConfig.java
new file mode 100644 (file)
index 0000000..5308709
--- /dev/null
@@ -0,0 +1,135 @@
+package de.juplo.yourshouter;
+
+
+
+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.social.config.annotation.ConnectionFactoryConfigurer;
+import org.springframework.social.config.annotation.EnableSocial;
+import org.springframework.social.config.annotation.SocialConfigurerAdapter;
+import org.springframework.social.connect.Connection;
+import org.springframework.social.connect.ConnectionFactoryLocator;
+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.facebook.api.Facebook;
+import org.springframework.social.facebook.connect.FacebookConnectionFactory;
+
+
+/**
+ * Spring Social Configuration.
+ *
+ * @author Kai Moritz
+ */
+@Configuration
+@EnableSocial
+public class SocialConfig extends SocialConfigurerAdapter
+{
+  /**
+   * Add a {@link FacebookConnectionFactory} to the configuration.
+   * The factory is configured through the keys <code>facebook.app.id</code>
+   * and <code>facebook.app.secret</code>.
+   *
+   * @param config
+   * @param env 
+   */
+  @Override
+  public void addConnectionFactories(
+      ConnectionFactoryConfigurer config,
+      Environment env
+      )
+  {
+    config.addConnectionFactory(
+        new FacebookConnectionFactory(
+            env.getProperty("facebook.app.id"),
+            env.getProperty("facebook.app.secret")
+            )
+        );
+  }
+
+  /**
+   * Configure an instance of {@link InMemoryUsersConnection} as persistent
+   * store of user/connection-mappings.
+   *
+   * At the moment, no special configuration is needed.
+   *
+   * @param connectionFactoryLocator
+   *     The {@link ConnectionFactoryLocator} will be injected by Spring.
+   * @return
+   *     The configured {@link UsersConnectionRepository}.
+   */
+  @Override
+  public UsersConnectionRepository getUsersConnectionRepository(
+      ConnectionFactoryLocator connectionFactoryLocator
+      )
+  {
+    InMemoryUsersConnectionRepository repository =
+        new InMemoryUsersConnectionRepository(connectionFactoryLocator);
+    return repository;
+  }
+
+  /**
+   * Configure a {@link UserIdSource}, that is equivalent to the one, that is
+   * created by Spring-Boot.
+   *
+   * @return
+   *     An instance of {@link AnonymousUserIdSource}.
+   *
+   * @see {@link AnonymousUserIdSource}
+   */
+  @Override
+  public UserIdSource getUserIdSource()
+  {
+    return new AnonymousUserIdSource();
+  }
+
+
+  /**
+   * Configuration of the controller, that handles the authorization against
+   * the Facebook-API, to connect a user to Facebook.
+   *
+   * At the moment, no special configuration is needed.
+   *
+   * @param factoryLocator
+   *     The {@link ConnectionFactoryLocator} will be injected by Spring.
+   * @param repository
+   *     The {@link ConnectionRepository} will be injected by Spring.
+   * @return
+   *     The configured controller.
+   */
+  @Bean
+  public ConnectController connectController(
+      ConnectionFactoryLocator factoryLocator,
+      ConnectionRepository repository
+      )
+  {
+    ConnectController controller =
+        new ConnectController(factoryLocator, repository);
+    return controller;
+  }
+
+  /**
+   * Configure a scoped bean named <code>facebook</code>, that enables
+   * access to the Graph-API in the name of the current user.
+   *
+   * @param repository
+   *     The {@link ConnectionRepository} will be injected by Spring.
+   * @return
+   *     A {@Connection<Facebook>}, that represents the authorization of the
+   *     current user against the Graph-API, or <code>null</code>, if the
+   *     current user is not connected to the API.
+   */
+  @Bean
+  @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
+  public Facebook facebook(ConnectionRepository repository)
+  {
+    Connection<Facebook> connection =
+        repository.findPrimaryConnection(Facebook.class);
+    return connection != null ? connection.getApi() : null;
+  }
+}
index 80f6121..21463c2 100644 (file)
@@ -1,5 +1,5 @@
-spring.social.facebook.appId=@facebook.app.id@
-spring.social.facebook.appSecret=@facebook.app.secret@
+facebook.app.id=@facebook.app.id@
+facebook.app.secret=@facebook.app.secret@
 
 spring.thymeleaf.prefix=/thymeleaf/
 spring.thymeleaf.cache=false