Authenticate against Facebook
authorKai Moritz <kai@juplo.de>
Fri, 15 Jan 2016 16:30:00 +0000 (17:30 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 30 Jan 2016 13:35:07 +0000 (14:35 +0100)
Based on the "Getting Started"-example:
http://spring.io/guides/gs/accessing-facebook/

.gitignore [new file with mode: 0644]
pom.xml [new file with mode: 0644]
src/main/java/de/juplo/yourshouter/Application.java [new file with mode: 0644]
src/main/java/de/juplo/yourshouter/HomeController.java [new file with mode: 0644]
src/main/resources/application.properties [new file with mode: 0644]
src/main/resources/templates/connect/facebookConnect.html [new file with mode: 0644]
src/main/resources/templates/connect/facebookConnected.html [new file with mode: 0644]
src/main/resources/templates/home.html [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..b83d222
--- /dev/null
@@ -0,0 +1 @@
+/target/
diff --git a/pom.xml b/pom.xml
new file mode 100644 (file)
index 0000000..c025b24
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
+  xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="
+    http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
+  ">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>de.juplo.examples</groupId>
+  <artifactId>facebook-app</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <parent>
+    <groupId>org.springframework.boot</groupId>
+    <artifactId>spring-boot-starter-parent</artifactId>
+    <version>1.3.1.RELEASE</version>
+  </parent>
+
+  <organization>
+    <name>juplo - Java bits from nerds for nerds</name>
+    <url>http://juplo.de</url>
+  </organization>
+
+  <properties>
+    <java.version>1.8</java.version>
+    <!-- settings for the Graph-API -->
+    <facebook.app.id>NOT_SET</facebook.app.id>
+    <facebook.app.secret>NOT_SET</facebook.app.secret>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-thymeleaf</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.social</groupId>
+      <artifactId>spring-social-facebook</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-maven-plugin</artifactId>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
diff --git a/src/main/java/de/juplo/yourshouter/Application.java b/src/main/java/de/juplo/yourshouter/Application.java
new file mode 100644 (file)
index 0000000..2263d18
--- /dev/null
@@ -0,0 +1,14 @@
+package de.juplo.yourshouter;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+
+@SpringBootApplication
+public class Application
+{
+  public static void main(String[] args)
+  {
+    SpringApplication.run(Application.class, args);
+  }
+}
\ No newline at end of file
diff --git a/src/main/java/de/juplo/yourshouter/HomeController.java b/src/main/java/de/juplo/yourshouter/HomeController.java
new file mode 100644 (file)
index 0000000..bb371f4
--- /dev/null
@@ -0,0 +1,61 @@
+package de.juplo.yourshouter;
+
+import javax.inject.Inject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.springframework.social.facebook.api.Facebook;
+import org.springframework.social.facebook.api.User;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+
+/**
+ * Controller, that handles the welcome-page.
+ *
+ * @author Kai Moritz
+ */
+@Controller
+@RequestMapping("/")
+public class HomeController
+{
+  private final static Logger LOG = LoggerFactory.getLogger(HomeController.class);
+
+
+  private final Facebook facebook;
+
+
+  @Inject
+  public HomeController(Facebook facebook)
+  {
+    this.facebook = facebook;
+  }
+
+
+  @RequestMapping(method = RequestMethod.GET)
+  public String home(Model model)
+  {
+    boolean authorized = true;
+    try
+    {
+      authorized = facebook.isAuthorized();
+    }
+    catch (NullPointerException e)
+    {
+      LOG.debug("NPE while acessing Facebook: {}", e);
+      authorized = false;
+    }
+    if (!authorized)
+    {
+      LOG.info("no authorized user, redirecting to /connect/facebook");
+      return "redirect:/connect/facebook";
+    }
+
+    User user = facebook.userOperations().getUserProfile();
+    LOG.info("authorized user {}, id: {}", user.getName(), user.getId());
+    model.addAttribute("user", user);
+    return "home";
+  }
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
new file mode 100644 (file)
index 0000000..b5ee75e
--- /dev/null
@@ -0,0 +1,5 @@
+spring.social.facebook.appId=@facebook.app.id@
+spring.social.facebook.appSecret=@facebook.app.secret@
+
+logging.level.org.springframework.social=debug
+logging.level.de.juplo.yourshouter=info
diff --git a/src/main/resources/templates/connect/facebookConnect.html b/src/main/resources/templates/connect/facebookConnect.html
new file mode 100644 (file)
index 0000000..5e275d8
--- /dev/null
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title>Connect to Facebook</title>
+  </head>
+  <body>
+    <h1>Connect to Facebook</h1>
+    <form action="#" th:action="@{/connect/facebook}" method="POST">
+      <div class="formInfo">
+        <p>
+          You aren't connected to Facebook yet.
+          Click the button to connect with your Facebook account.
+        </p>
+      </div>
+      <p><button type="submit">Connect to Facebook</button></p>
+    </form>
+  </body>
+</html>
diff --git a/src/main/resources/templates/connect/facebookConnected.html b/src/main/resources/templates/connect/facebookConnected.html
new file mode 100644 (file)
index 0000000..6c28e0d
--- /dev/null
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title>Connect to Facebook</title>
+  </head>
+  <body>
+    <p>Back <a href="home.html" th:href="@{/}">HOME</a></p>
+    <hr />
+    <h1>Connected to Facebook</h1>
+    <p>
+      You are now connected to your Facebook account.
+    </p>               
+  </body>
+</html>
diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html
new file mode 100644 (file)
index 0000000..04d9d8a
--- /dev/null
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title>Home</title>
+  </head>
+  <body>
+    <h1>Hello, <span th:text="${user.name}">Some User</span>!</h1>
+    <ul>
+      <li><a href="connect/facebookConnected.html" th:href="@{/connect/facebook.html}">Show connection-status</a></li>
+    </ul>
+  </body>
+</html>