EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot OAuth2
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot OAuth2

Spring Boot OAuth2

Definition of Spring Boot OAuth2

In Spring boot, we have one mechanism which helps us to do Authorization; this is called as oauth2.0; by the use of this, we can easily authorize the interaction between two services. The main purpose of oauth2 is to authorize two services on behalf of the user who has access to the resource. oauth2 is not meant for authentication rather;, it is used for authorization. We have a resource; resource own we, authorization server, resource server, and client when it comes to the important terminology to the oauth2. All this entity plays an important role in oauth2; they are supposed to play a specific part of the oauth2 flow. Mainly we have three types of flow for oauth2, but in this tutorial, we will cover the only one which is most widely used and secure. In the coming section of the tutorial, we will see how we can implement oauth2 in our application to provide an authorization mechanism, also a closer look what are the basic steps needed to achieve this in the spring boot application.

Syntax:

As we know that oauth2 is a mechanism that requires configuration in place in order to make it work. In this section, we will see basic configuration, which is very much required to start with oauth2 in spring boot see below;

@EnableWebSecurity
public class class_name extends WebSecurityConfigurerAdapter {
// logic goes here
}

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

As you can see in the above line of code, we are trying to create the security class and trying to enable web security in order to implement this. Let’s take a closer look at the practice syntax for better understanding see below;

e.g. :

@EnableWebSecurity
public class DemoConfig extends WebSecurityConfigurerAdapter {
// logic goes here
}

In the coming section, we will see in detail what are the steps needed to implement this properly in spring boot applications for better understanding for beginners.

How does Spring boot oauth2 work?

As we have already known that in spring boot, we can implement oauth2 to authorize the user, it basically meant for authorization, not for authentication. Also, the primary function of oauth2 is to authorize the user. Its basic purpose is to authorize the two different services required to access some resource from then on behalf of the user with has the access. In this section of the tutorial, we will see one problem statement and try to understand it better. As we have already known that oauth2 has various terminology which will give us a basic understanding of the flow and how it works internally; let’s get started;

1) Resource: This is the resource that we want to access, and for this, we want the authorization. It is called a protected resource as well.

2) Resource owner: This is the entity that has access to the protected resource, which means who can allow us to grant access to the resource we want to access or any other service want to access.

3) Resource server: This is the server that has the protected resource with it, and the other service wants to access it, but they have the valid authorization or access in order to use the resource.

4) Client: This is often term as a client means the one who wants to access the protected resource on behalf of the user. If the user gives the permission, then it can access otherwise, no.

5) Authorization server: This is the one that is very important, and it can be coupled with the resource server, or it can be the single server, which is running alone to authorize the other service and provide them the resource. But this server has to provide all kinds of security and should allow someone who is to authorize on behalf of a user.
Let’s understand one problem statement to get this type of situation; in the below diagram, we have to state where we have user and two different services; they want to communicate with one other let’s get started to see below;

1) User has some data which resided on service two. But the user directly intercepts with service one, not with service at the moment. So user told service 1 to get the data from service 2.

2) In this case, service 1 and service 2 both do not know each other who they are.

3) Service 1 told service two that I want data of this user from you can you provide me? But service 2 does not know, and it will say I cannot provide without the user consent.

4) So now it will ask the user that service 1 wants to access your personal data, which resides on my server should I allow him access? or is this a valid URL?

5) Now, users will say yes, allow access to it is to the true one, allow them to access my data.

6) In this way, oauth2 works, in general, to allow the access of resources on behalf of the user.
To solve this problem, we have oauth2, which protects and secures our data from the server and only authorizes it when it comes from the right location with the consent of the user if the user allows it. So it is very secure, readable, and easy to understand as well.

Let’s take and look at the flow chart details how it exactly works in the scenarios to exchange the token, in order to get the details or data from the other service see below;

1) In the first step user will tell service 1 to get the data from service 2.

2) Now, in this step, it will go to the authorization server to request access in order to access the resource.

3) But the authentication server does not know because he only trusts his user, so it will again go back to the user and ask him if he wants to give access to this service.

4) Now, it depends on the user whether he wants to allow the access or not. In this case, it will allow and send this to an authorization server.

5) Now, the authorization server has cross-checked it with the user and got his permission to allow access to the service. Now the authorization server will give it an auth token in der to access the resource on the server.

6) With this auth token, now service 1 has to make one request to get the access token. S service 1 will pass this auth token to the authorization service and get the access token to access the actual resource on the server.

7) Now, the authorization server will give it back an access token in return

8) Now, this is the final step where service 1 will call the API endpoint to get the data of the user with the help of this access token.

This is how the flow of oauth2 works in the spring boot application. And we have already seen it is very much secure a well. in order to get the resource with the consent of the user, if he allows it, we cannot go and access it.

Conclusion

As we have already seen, it is easy to use, and how it works internally to authorize the external service on behalf of the user. Just make the small configuration and make this work properly. But before that, its working was important how it works in spring boot.

Recommended Articles

This is a guide to Spring Boot OAuth2. Here we discuss the Definition, syntax, How Spring boot oauth2 works? example with code implementation. You may also have a look at the following articles to learn more –

  1. Spring Boot application. properties
  2. Spring Boot Run Command
  3. Spring cloud stream
  4. Spring Boot Profiles
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more