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:
After opening, log in with your Gmail account and you'll see your Google Developer Console Dashboard:
Create a New Project
Create a new project as shown below:
Select Credentials
After creating the project, select your project and then select Credentials as shown below:
Create OAuth Client ID
Now select Create Credential and then choose OAuth Client ID:
Configure Consent Screen
Next, select Configure Consent Screen:
Then select External and click the Create button:
Fill Details and Save
Fill in the required details and save:
Get Client ID and Secret
After saving everything, select Credentials again and fill in some details:
After saving, you'll get your Client ID and Client Secret:
Step 2: Create Spring Boot Project
Now create a project in Spring Tool Suite (STS) as shown below:
Add the following dependencies and click Finish:
Project Structure
Create the project structure as shown below:
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:
After this, you can run your project and you'll see the following page:
Then log in with your email and password, and you'll be able to access the 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()inSecurityConfig - 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:
Post a Comment