Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Enhance Security with OAuth 2.0: Implementing Social Logins in Spring Boot

by Ayush Shrivastava

.

Updated Sat Jun 21 2025

.
Enhance Security with OAuth 2.0: Implementing Social Logins in Spring Boot

In modern web development, securing applications while providing smooth user experiences is crucial. One way to achieve both is by using OAuth 2.0, a powerful protocol that not only secures your APIs but also simplifies user authentication by allowing users to log in with their existing accounts, like those from Google and GitHub. This streamlines the login process: users don’t need to remember another password, and developers get a reliable, secure way to manage authentication.

In this guide, I’ll walk you through setting up OAuth 2.0 authentication in a Spring Boot application. We’ll integrate both Google and GitHub for authentication, giving users a choice of login services. I’ll also show you how to protect your API endpoints using JWT (JSON Web Tokens), ensuring that only authenticated users can access your resources.

Whether you’re building a new app or enhancing the security of an existing one, this guide will equip you with the tools to make your Spring Boot application secure and user-friendly.

Getting Started

Visit start.spring.io to create your Spring Boot project.

rest.webpDependencies

To integrate OAuth 2.0 authentication with Google and GitHub, your Spring Boot project will need the following two key dependencies:

  1. OAuth2 Client: This dependency in simplifies the integration of OAuth 2.0 authentication with providers like Google and GitHub. It automates the entire OAuth login process, from redirecting users to the provider's login page to managing authentication tokens and securing API endpoints. By adding this dependency, you can quickly enable a secure and user-friendly authentication system in your Spring Boot application.

  2. Spring Web: This dependency is essential for developing web applications with Spring Boot. It includes critical features such as RESTful API creation, support for the MVC architecture, and the ability to serve HTML views. With Spring Web, you can effectively handle HTTP requests, manage routing, and integrate seamlessly with other Spring components, making it a cornerstone for building robust web applications.

Application Configuration

To set up your Spring Boot application for OAuth 2.0 authentication with Google and GitHub, you'll need to configure the application.properties file. This file contains essential settings for your application, including OAuth client credentials, logging levels, and JWT configurations.

spring.application.name=oauth2-authentication-service
server.port=8000

#for google
spring.security.oauth2.client.registration.google.client-id=YOUR_GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_GOOGLE_CLIENT_SECRET

#for github
spring.security.oauth2.client.registration.github.client-id=YOUR_GITHUB_CLIENT_ID
spring.security.oauth2.client.registration.github.client-secret= YOUR_GITHUB_CLIENT_SECRET

OAuth Client Configurations: Replace YOUR_GOOGLE_CLIENT_ID, YOUR_GOOGLE_CLIENT_SECRET, YOUR_GITHUB_CLIENT_ID, and YOUR_GITHUB_CLIENT_SECRET with the credentials you obtain from Google and GitHub when you register your application.

To register your application with Google and GitHub for OAuth 2.0 authentication, we need to go https://console.cloud.google.com

rest1.webpClick on API Services

rest2.webpCredentials -> create Credentials -> OAuth client ID

rest3.webpOAuth client ID -> Create OAuth client ID

rest4.webpSelect Application type to web Application

rest5.webpWrite the name of application

rest6.webpSet Authorized redirect URIs with this URL and here our application is running on 8000 port so application port is 8000. then click on create

http://localhost:8000/login/oauth2/code/google

rest7.webpOnce the OAuth client is created, you will receive the Client ID and Client Secret.

0xa2daioqoaldju4kc7o.webpCopy both the Client ID and Client Secret, and replace the corresponding values in the application.properties file.

spring.security.oauth2.client.registration.google.client-id=YOUR_GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_GOOGLE_CLIENT_SECRET

The SecurityConfig class configures security for a Spring Boot application using OAuth2. It defines a SecurityFilterChain bean, which sets up security rules. The authorizeHttpRequests method ensures that all incoming requests require authentication.

The .oauth2Login(Customizer.withDefaults()) line enables OAuth2 login functionality with default settings. Finally, the securityFilterChain method returns the configured security filter chain by calling http.build(). This setup ensures that the application is secure and supports OAuth2 authentication for users.

Accessing Your Application via Chrome

When developing and testing your Spring Boot application, it's crucial to know how to interact with it through Postman. If your application is running locally on port 8000, you can access it using the following base URL:

http://localhost:8000

rest8.webpWe get the similar response like this

rest9.webpNow we can access the end points.

GitHub Authentication

GitHub Authentication in Spring Boot enables users to log in using their GitHub accounts, streamlining the authentication process and enhancing security. By integrating GitHub as an OAuth 2.0 provider, your application can authenticate users through GitHub's trusted platform. This involves registering your application on GitHub to obtain a Client ID and Client Secret, which are then configured in your Spring Boot application. Users are redirected to GitHub for login, and upon successful authentication, they are redirected back to your application with an access token, allowing secure access to your protected resources. This integration is ideal for applications targeting developers and tech-savvy users.

Create GitHub account and go to settings

git.webpIn the left corner, get the developer settings

git1.webpNavigate to OAuth Apps

git2.webpClick on create OAuth App

git3.webpYou’ll get an interface like this

git4.webpSet Authorization callback URL according to your application port

http://localhost:8000/login/oauth2/code/github

git5.webpSet Homepage URL

<http://localhost:8000>

After registering the Application we get the Client ID and Client Secret

git6.webpNow replace with the Application.properties file properties

spring.security.oauth2.client.registration.github.client-id=Ov23liBMLc5e1ItoONPx
spring.security.oauth2.client.registration.github.client-secret=

git7.webpTest the GitHub Login

Login with GitHub: When prompted, log in with your GitHub credentials. Success Redirect: Upon successful authentication, you'll be redirected to the /home page of your application.

git8.webpYou can explore the complete source code for the User Authentication Service on my GitHub repository. This project showcases various features such as user registration, login, and secure access using JWT for authentication. Feel free to check it out, contribute, or use it as a reference for your own projects!

https://github.com/ishrivasayush/oauth2-authentication-service

Conclusion

Implementing OAuth 2.0 with Spring Boot, using Google and GitHub as authentication providers, is a powerful way to enhance the security and usability of your application. By allowing users to log in with their existing accounts, you reduce friction and provide a smoother user experience. At the same time, securing your API endpoints with JWT ensures that only authenticated users have access to sensitive resources.

Through this guide, we’ve covered everything from setting up OAuth credentials on Google and GitHub to configuring your Spring Boot application to handle authentication and protect your endpoints. Whether you’re new to OAuth 2.0 or looking to integrate it into your projects, these steps will help you build a secure and scalable authentication system.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board

Backend Tips, Every week

Backend Tips, Every week