DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Authentication With Remote LDAP Server in Spring Web MVC
  • Authentication With Remote LDAP Server in Spring WebFlux
  • How to Implement Two-Factor Authentication in A Spring Boot OAuth Server? Part 2: Under the Hood
  • Google Cloud Pub/Sub: Messaging With Spring Boot 2.5

Trending

  • API Standards Are Data Standards
  • Testing Java Applications With WireMock and Spring Boot
  • Debug Like a Pro in 2025: 10 New Eclipse Java Debugger Features to Enhance Your Productivity (With Spring Boot Examples)
  • How to Reduce Technical Debt With Artificial Intelligence (AI)
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Security Oauth2: Google Login

Spring Security Oauth2: Google Login

In this post, learn about implementing spring security oauth2 and authenticating your users using the Google authorization service.

By 
Amit Mishra user avatar
Amit Mishra
DZone Core CORE ·
Jan. 03, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
9.2K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

With the evolution of technologies, we also need to take care of security for our web applications. In this post, we're going to talk about one of the ways to secure your spring boot applications using spring-security-oauth2.  I will demonstrate how we can authenticate the user with their Google accounts using the oauth2 stack in spring security 5.

Getting Started

We will first create our project and make sure your Pom.xml should at least have the dependencies that I have below. 

XML
 




xxxxxxxxxx
1


 
1
        <dependency>
2
            <groupId>org.springframework.boot</groupId>
3
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
4
        </dependency>
5
        <dependency>
6
            <groupId>org.springframework.boot</groupId>
7
            <artifactId>spring-boot-starter-web</artifactId>
8
        </dependency>



I have used the spring boot starter oauth2 client for this article, but now everything is there in spring security 5, and we can use the OAuth support within spring security. 

Configuring Your Application

The first thing that you need to do is you will need to create your application in Google console to tell Google that I have an application that will be using your authorization services.  

Visit Google API Console and sign in with your Google account if you are not signed in.  You need to go to credentials tab -> Create Credentials -> Oauth2 ClientID . 

It will then ask you to select what type of application you're going to build.  Select web application -> Application Name. You can set up a redirect URI if you want(Please make sure if you're configuring a redirect URI in google console then you will also need to include that in spring-boot configuration). 

Once you've entered the details, click on create an application. It will give you some alphanumeric ClientID and Client-Secret.

Configuring Your Spring Boot Application

Once you've set up your ClientID and Client-Secret from Google, we're good to continue integrating this with our spring-boot application.  You now just need to add a few properties in your spring boot application as shown below.

YAML
 




x


 
1
spring:
2
  security:
3
    oauth2:
4
      client:
5
        registration:
6
          google:
7
            client-id: your-client-ID.apps.googleusercontent.com
8
            client-secret: Your-Client-Secret-Key
9
            redirect-uri: Redirect-Uri that you've configured
10

          
11

          



Run your application and go to the server address, and try to access an endpoint. It will redirect you to the Google login page, and after logging in, you will be able to access protected resources.  The code is available on the github-repository. With that being said, thank you so much for taking the time to read this article. I hope this will help you in some way.

Spring Framework Spring Security Google (verb) authentication application Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Authentication With Remote LDAP Server in Spring Web MVC
  • Authentication With Remote LDAP Server in Spring WebFlux
  • How to Implement Two-Factor Authentication in A Spring Boot OAuth Server? Part 2: Under the Hood
  • Google Cloud Pub/Sub: Messaging With Spring Boot 2.5

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: