From 174c0e31b6114e9ff7a8012e0a0ac3bfdaad0a88 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 15 Jan 2016 17:30:00 +0100 Subject: [PATCH 1/1] Authenticate against Facebook Based on the "Getting Started"-example: http://spring.io/guides/gs/accessing-facebook/ --- .gitignore | 1 + pom.xml | 57 +++++++++++++++++ .../de/juplo/yourshouter/Application.java | 14 +++++ .../de/juplo/yourshouter/HomeController.java | 61 +++++++++++++++++++ src/main/resources/application.properties | 5 ++ .../templates/connect/facebookConnect.html | 18 ++++++ .../templates/connect/facebookConnected.html | 14 +++++ src/main/resources/templates/home.html | 12 ++++ 8 files changed, 182 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/de/juplo/yourshouter/Application.java create mode 100644 src/main/java/de/juplo/yourshouter/HomeController.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/templates/connect/facebookConnect.html create mode 100644 src/main/resources/templates/connect/facebookConnected.html create mode 100644 src/main/resources/templates/home.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c025b24 --- /dev/null +++ b/pom.xml @@ -0,0 +1,57 @@ + + + + 4.0.0 + + de.juplo.examples + facebook-app + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + + juplo - Java bits from nerds for nerds + http://juplo.de + + + + 1.8 + + NOT_SET + NOT_SET + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.social + spring-social-facebook + + + org.slf4j + slf4j-api + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/de/juplo/yourshouter/Application.java b/src/main/java/de/juplo/yourshouter/Application.java new file mode 100644 index 0000000..2263d18 --- /dev/null +++ b/src/main/java/de/juplo/yourshouter/Application.java @@ -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 index 0000000..bb371f4 --- /dev/null +++ b/src/main/java/de/juplo/yourshouter/HomeController.java @@ -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 index 0000000..b5ee75e --- /dev/null +++ b/src/main/resources/application.properties @@ -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 index 0000000..5e275d8 --- /dev/null +++ b/src/main/resources/templates/connect/facebookConnect.html @@ -0,0 +1,18 @@ + + + + Connect to Facebook + + +

Connect to Facebook

+
+
+

+ You aren't connected to Facebook yet. + Click the button to connect with your Facebook account. +

+
+

+
+ + diff --git a/src/main/resources/templates/connect/facebookConnected.html b/src/main/resources/templates/connect/facebookConnected.html new file mode 100644 index 0000000..6c28e0d --- /dev/null +++ b/src/main/resources/templates/connect/facebookConnected.html @@ -0,0 +1,14 @@ + + + + Connect to Facebook + + +

Back HOME

+
+

Connected to Facebook

+

+ You are now connected to your Facebook account. +

+ + diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html new file mode 100644 index 0000000..04d9d8a --- /dev/null +++ b/src/main/resources/templates/home.html @@ -0,0 +1,12 @@ + + + + Home + + +

Hello, Some User!

+ + + -- 2.20.1