Spring Boot OAuth2 Tutorial with Google

Spring Boot OAuth2 Google Login Tutorial | Google Sign-In with Spring Security

Spring Boot OAuth2 Google Login Tutorial

Author: Deep Singh | Category: Java, Spring Boot, Spring Security, OAuth2

Introduction

In this tutorial, you'll learn how to implement Google OAuth2 login in a Spring Boot application using Spring Security. We'll walk through the entire process — from setting up a project in the Google Developer Console to configuring oauth2Login() in Spring Security.

By the end, you'll have a working Spring Boot app where users can sign in with their Google account.

Step 1: Google Developer Console Setup

Open Google Developer Console

First, open the Google Developer Console from the link below:

Google Developer Console

After opening, log in with your Gmail account and you'll see your Google Developer Console Dashboard:

Google Developer Console dashboard after logging in with a Gmail account
Figure 1: Google Developer Console dashboard

Create a New Project

Create a new project as shown below:

Creating a new project in Google Developer Console
Figure 2: Create a new project

Select Credentials

After creating the project, select your project and then select Credentials as shown below:

Selecting Credentials in Google Developer Console
Figure 3: Select Credentials

Create OAuth Client ID

Now select Create Credential and then choose OAuth Client ID:

Selecting OAuth Client ID from the Create Credential dropdown
Figure 4: Create OAuth Client ID

Fill Details and Save

Fill in the required details and save:

Filling OAuth consent screen details in Google Developer Console
Figure 7: Fill consent screen details
Saving OAuth consent screen configuration
Figure 8: Save consent screen

Get Client ID and Secret

After saving everything, select Credentials again and fill in some details:

Filling OAuth client ID details in Google Developer Console
Figure 9: Fill OAuth client ID details

After saving, you'll get your Client ID and Client Secret:

Google OAuth Client ID and Client Secret displayed in Google Developer Console
Figure 10: Client ID and Client Secret

Step 2: Create Spring Boot Project

Now create a project in Spring Tool Suite (STS) as shown below:

Creating a new Spring Boot project in Spring Tool Suite
Figure 11: Create Spring Boot project

Add the following dependencies and click Finish:

Adding Spring Boot dependencies including Spring Security and OAuth2 Client
Figure 12: Add dependencies

Project Structure

Create the project structure as shown below:

Spring Boot OAuth2 project structure showing config, controller, and main application packages
Figure 13: Spring Boot project structure

OuthTutorialGoogleApplication.java

package com.deepsingh44.blogspot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OuthTutorialGoogleApplication {

    public static void main(String[] args) {
        SpringApplication.run(OuthTutorialGoogleApplication.class, args);
    }
}

SecurityConfig.java

package com.deepsingh44.blogspot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**")
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .oauth2Login()
            .and()
            .logout();
    }
}

MainController.java

package com.deepsingh44.blogspot.controller;

import java.security.Principal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {

    @GetMapping("/")
    public String home(Principal principal) {
        return "Welcome, " + principal.getName();
    }
}

application.properties

server.port=9988
spring.security.oauth2.client.registration.google.client-id=xxxxxxxxxxxxxxx
spring.security.oauth2.client.registration.google.client-secret=xxxxxxxxxxx

Note: Replace the client-id and client-secret placeholder values with the credentials you obtained from the Google Developer Console in Step 1.

Step 3: Run and Test the Project

Run your project. If you get an error, solve it by pasting the URL in the Google Developer Console and saving it as shown below:

Adding authorized redirect URI in Google Developer Console to fix redirect error
Figure 14: Add authorized redirect URI

After this, you can run your project and you'll see the following page:

Spring Boot OAuth2 Google login page with Sign in with Google option
Figure 15: Google login page

Then log in with your email and password, and you'll be able to access the main page:

Spring Boot main page displaying welcome message with authenticated user's name
Figure 16: Authenticated main page

Conclusion

In this tutorial, we successfully implemented Google OAuth2 login in a Spring Boot application. We covered the complete workflow:

  • Setting up a project in the Google Developer Console
  • Configuring the OAuth consent screen
  • Creating an OAuth Client ID and obtaining the Client Secret
  • Building a Spring Boot project with Spring Security and OAuth2 Client dependencies
  • Enabling Google login using oauth2Login() in SecurityConfig
  • Accessing the authenticated user's information via Principal

Key takeaways:

  • Spring Security provides out-of-the-box oauth2Login() support, making social login extremely simple.
  • The Google Developer Console is where you register your app and obtain OAuth2 credentials.
  • Client ID and Client Secret are configured in application.properties.
  • The redirect URI must be registered in the Google Console to avoid redirect errors.

You can extend this example by adding support for multiple OAuth2 providers (GitHub, Facebook, etc.), storing user details in a database, or customizing the login page. Spring Boot's OAuth2 support makes it easy to build secure, modern authentication flows with minimal configuration.

No comments: