SpringBoot

Spring Boot 3.0 Facebook Login

Jason / 莊子政 Jason Chuang 2023/01/12 08:30:29
1219

 

spring-boot-3.0-facebook-login

By Jason Tzu-Cheng Chuang 2023-01-12

Purpose

In this tutorial, I’d love to share with you guys about implementing social login with Facebook for an existing Spring Boot web application, using Spring OAuth2 Client library – so your users will be able to sign in your application using their own Facebook accounts instead of application-managed credentials.

Suppose that you have an existing Spring Boot project with authentication functionality already implemented using Spring Security and the user information is stored in H2 in-memory database.

Then we will update the login page that lets the users login using their own Facebook accounts like this:

facebook login

Software version

Java 17

spring boot 3.0.1

spring security 6

thymeleaf-extras-springsecurity6

Github Source Code

https://github.com/chuangtc/spring-boot-3.0-facebook-login

Folder structure

folder structure

Maven Configuration

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

The Facebook Properties yml file

Next, let's configure Facebook properties in our application.yml:

  security:
    oauth2:
      client:
        registration:
         facebook:
          clientId: YOUR_APP_ID
          clientSecret: YOUR_APP_SECRET
          scope:
           - email
           - public_profile

Conclusion

In this quick article, we learned how to use spring-boot-starter-security to implement a secondary authentication flow for our application.

Relevant Articles:

Reference

 

Jason