VOOZH about

URL: https://dzone.com/articles/spring-security-oauth2-google-login

⇱ Spring Security Oauth2: Google Login


Related

  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 Jan. 03, 21 · Tutorial
Likes
Comment
Save
9.8K 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.

Let's be friends: