You are currently viewing Spring Boot OAuth2 Social Login with Google, Facebook Twitter

Spring Boot OAuth2 Social Login with Google, Facebook Twitter

OAuth2 Social Login is a common feature in modern web applications, allowing users to sign in using their social media accounts. Spring Boot provides robust support for implementing OAuth2 authentication with popular social platforms like Facebook, Google, and GitHub. In this tutorial, we’ll guide you through the process of setting up OAuth2 Social Login in a Spring Boot application, enabling users to log in using their preferred social accounts.


  • Basic understanding of Spring Boot
  • JDK installed on your machine
  • Maven or Gradle installed
  • Developer accounts on Google, Facebook, and Twitter (to obtain client credentials)

Step 1: Create a Spring Boot Project

You can create a new Spring Boot project using the Spring Initializr, or you can use your preferred IDE to generate a new project with the necessary dependencies.

For Maven, add the following dependencies:

  • Spring Web
  • Spring Security
  • Spring Boot OAuth2 Client
  • Thymeleaf (for simplicity in this example)

Here’s an example pom.xml:

    <!-- Spring Boot Starter Web -->

    <!-- Spring Security -->

    <!-- Spring Boot OAuth2 Client -->

    <!-- Thymeleaf for templating -->

Step 2: Configure OAuth2 Providers

Now, we need to configure our application to work with Google, Facebook, and Twitter as OAuth2 providers. You will need to obtain client credentials (client ID and secret) from each provider’s developer console.

Google Configuration

Create a file or application.yml file in src/main/resources:,email

Facebook Configuration

Twitter Configuration

Step 3: Create a Controller

Now let’s create a controller to handle the login and redirect URLs:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

public class LoginController {

    public String index() {
        return "index";

    public String login() {
        return "login";

    public String loginError(Model model) {
        model.addAttribute("loginError", true);
        return "login";

Step 4: Create Login Template (login.html)

Next, create a Thymeleaf template for the login page (src/main/resources/templates/login.html):

<!DOCTYPE html>
<html xmlns:th="">
    <h2>Login with Social Accounts</h2>
    <div th:if="${loginError}" class="alert alert-danger">
        Invalid username or password.
        <a href="/login/oauth2/authorization/google" class="btn btn-primary">Login with Google</a>
        <a href="/login/oauth2/authorization/facebook" class="btn btn-primary">Login with Facebook</a>
        <a href="/login/oauth2/authorization/twitter" class="btn btn-primary">Login with Twitter</a>

Step 5: Test the Application

Now you can run your Spring Boot application (mvn spring-boot:run) and navigate to http://localhost:8080/. You should see the login page with buttons for Google, Facebook, and Twitter.

When a user clicks one of these buttons, they will be redirected to the respective OAuth2 provider’s login page. After successful authentication, they will be redirected back to your application.


In this tutorial, we’ve created a simple Spring Boot application that allows users to log in using their Google, Facebook, or Twitter accounts using OAuth2. We configured the application with client credentials for each provider, created a login page with buttons for social login, and handled the redirection after successful authentication. This is a basic example, and in a production environment, you would likely want to handle user registration, data storage, and more complex user flows.

Leave a Reply