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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • Applying Microservices Lessons to Multi-Agent AI Systems
  • The "Just Read the Code" Myth Is Killing Your AI Workflow
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Spring Security JWT Validation Best Practices

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jan. 03, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook