29 - Spring Security + OAuth 2
1. Introduction
OAuth 2.0 is an authorization framework that allows an application to access protected resources on behalf of a user, without requiring the application to know the userβs password.
In a Spring Boot application, OAuth 2.0 can be used to:
- Authenticate users through an external identity provider.
- Allow users to log in with Google, GitHub, or another provider.
- Protect APIs using access tokens.
- Delegate access to resources.
- Implement Single Sign-On (SSO).
The goal is to understand how OAuth 2.0 works and how to implement it using Spring Security.
2. What is OAuth 2.0?
OAuth 2.0 is an authorization framework that allows a client application to obtain limited access to protected resources.
Instead of sharing a userβs password with every application, the user authorizes an application to access specific resources.
Example
Imagine an application called Photo App that wants to access your Google Photos.
Without OAuth:
Photo App asks for your Google password
β
Photo App stores your password
β
Photo App accesses your photos
This is insecure.
With OAuth 2.0:
Photo App asks Google for permission
β
User authenticates with Google
β
User grants permission
β
Google issues an access token
β
Photo App uses the token to access photos
The application never needs to know the userβs Google password.
3. OAuth 2.0 vs Authentication
OAuth 2.0 was originally designed for authorization, not authentication.
Authorization
Answers:
What is this application allowed to access?
Example:
Photo App β Can read Google Photos
Authentication
Answers:
Who is the user?
Example:
User β Bruno Lima
OpenID Connect
OpenID Connect (OIDC) is an authentication protocol built on top of OAuth 2.0.
It adds an ID Token that contains information about the authenticated user.
Therefore:
OAuth 2.0 β Authorization
OpenID Connect β Authentication + Authorization
When you use:
Login with Google
You are typically using OpenID Connect, not OAuth 2.0 alone.
4. Example of OAuth 2.0
Imagine a user wants to connect a calendar application to their Google account.
Without OAuth
Calendar App
β
"Enter your Google password"
β
Calendar App accesses your account
With OAuth 2.0
Calendar App
β
Redirect user to Google
β
User logs in to Google
β
User authorizes Calendar App
β
Google redirects back with an authorization code
β
Calendar App exchanges code for tokens
β
Calendar App accesses the calendar API
The important idea is:
The application receives permission to access resources, not the userβs password.
5. Roles in OAuth 2.0 Architecture
OAuth 2.0 defines four main roles.
5.1 Resource Owner
The Resource Owner is the person or entity that owns the protected resources.
Example:
User Bruno
β
Owns Google Photos
The resource owner grants permission to the client.
5.2 Client
The Client is the application requesting access to protected resources.
Examples:
- Web application.
- Mobile application.
- Backend application.
- SPA (Single Page Application).
Example:
Photo App
β
Requests access to Google Photos
5.3 Authorization Server
The Authorization Server authenticates the resource owner and issues tokens to the client.
Responsibilities:
- Authenticate the user.
- Ask for consent.
- Validate authorization requests.
- Issue authorization codes.
- Issue access tokens.
- Issue refresh tokens.
Examples:
- Google Identity.
- Okta.
- Microsoft Entra ID.
- Keycloak.
- Auth0.
5.4 Resource Server
The Resource Server hosts the protected resources.
Examples:
- Google Photos API.
- Calendar API.
- Your Spring Boot REST API.
The resource server validates access tokens before allowing access.
6. OAuth 2.0 Architecture
+-------------------+
| Resource Owner |
| User |
+---------+---------+
|
| 1. Authorizes
v
+---------+---------+
| Client |
| Photo App |
+---------+---------+
|
| 2. Authorization Request
v
+---------+---------+
| Authorization |
| Server |
| Google / Okta |
+---------+---------+
|
| 3. Access Token
v
+---------+---------+
| Resource Server |
| Google Photos API |
+-------------------+
7. OAuth 2.0 Process Flow
The most common OAuth 2.0 flow is the Authorization Code Flow.
Step 1 β Client Requests Authorization
The client redirects the user to the authorization server.
Example:
https://authorization-server.com/authorize
?client_id=photo-app
&response_type=code
&redirect_uri=https://photo-app.com/login/oauth2/code/provider
&scope=photos.read
The request tells the authorization server:
- Which application is requesting access.
- Which response type is expected.
- Where to redirect the user.
- Which permissions are requested.
Step 2 β User Authenticates
The user logs in to the authorization server.
Example:
User
β
Google Login Page
β
Username + Password
β
Authentication Successful
The client application does not receive the userβs password.
Step 3 β User Grants Consent
The authorization server asks the user whether the client can access the requested resources.
Example:
Photo App wants to:
- View your photos
[Allow] [Deny]
If the user approves, the authorization server continues.
Step 4 β Authorization Server Returns an Authorization Code
The authorization server redirects the user back to the client.
Example:
https://photo-app.com/login/oauth2/code/provider
?code=abc123
&state=xyz789
The authorization code is temporary and is not the access token.
Step 5 β Client Exchanges the Code for Tokens
The client sends the authorization code to the token endpoint.
Example:
POST /oauth2/token
grant_type=authorization_code
code=abc123
redirect_uri=https://photo-app.com/login/oauth2/code/provider
client_id=photo-app
client_secret=client-secret
The authorization server validates the request.
Step 6 β Authorization Server Returns Tokens
Example response:
{
"access_token": "eyJhbGciOiJSUzI1NiIs...",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "def456",
"scope": "photos.read"
}
The client can now use the access token to access protected resources.
Step 7 β Client Accesses the Resource Server
The client sends the access token in the HTTP Authorization header.
GET /photos
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...
The resource server validates the token.
Step 8 β Resource Server Returns the Resource
If the token is valid and contains the required permissions:
HTTP/1.1 200 OK
[
{
"id": 1,
"name": "Vacation.jpg"
}
]
8. Complete OAuth 2.0 Flow
+-------------------+
| User |
+---------+---------+
|
| 1. Login / Authorization Request
v
+---------+---------+
| Client |
| Photo App |
+---------+---------+
|
| 2. Redirect to Authorization Server
v
+---------+---------+
| Authorization |
| Server |
+---------+---------+
|
| 3. User Authenticates
|
| 4. User Grants Consent
|
| 5. Authorization Code
v
+---------+---------+
| Client |
+---------+---------+
|
| 6. Exchange Code for Tokens
v
+---------+---------+
| Authorization |
| Server |
+---------+---------+
|
| 7. Access Token
v
+---------+---------+
| Client |
+---------+---------+
|
| 8. Bearer Token
v
+---------+---------+
| Resource Server |
+---------+---------+
|
| 9. Protected Resource
v
+-------------------+
| User |
+-------------------+
9. OAuth 2.0 Tokens
OAuth 2.0 commonly uses two types of tokens.
9.1 Access Token
The Access Token is used to access protected resources.
Example:
GET /api/profile
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...
The access token usually has a limited lifetime.
Example:
expires_in: 3600
This means the token expires after 3600 seconds.
9.2 Refresh Token
The Refresh Token is used to obtain a new access token without requiring the user to authenticate again.
Example:
POST /oauth2/token
grant_type=refresh_token
refresh_token=def456
The authorization server may return a new access token.
{
"access_token": "new-access-token",
"token_type": "Bearer",
"expires_in": 3600
}
Access Token vs Refresh Token
Access Token
β
Access protected resources
β
Short lifetime
Refresh Token
β
Obtain new access token
β
Longer lifetime
10. OAuth 2.0 Scopes
A Scope defines the permissions requested by the client.
Example:
scope=photos.read photos.write
This means the client is requesting permission to:
- Read photos.
- Write photos.
Example Scopes
profile
email
openid
photos.read
photos.write
calendar.read
Scopes are used to limit what the client can access.
Example:
Client has photos.read
β
Can read photos
β
Cannot delete photos
11. OAuth 2.0 Grant Types
A Grant Type defines how the client obtains an access token.
Authorization Code
Used when a user authorizes an application.
Example:
Web Application
β
User Login
β
Authorization Code
β
Access Token
This is the most common flow for user authentication.
Client Credentials
Used for machine-to-machine communication.
Example:
Service A
β
Requests token
β
Authorization Server
β
Access Token
β
Service B
There is no user involved.
Example:
Order Service β Inventory Service
Refresh Token
Used to obtain a new access token.
Example:
Refresh Token
β
Authorization Server
β
New Access Token
Resource Owner Password Credentials
This flow allowed clients to collect the userβs username and password directly.
It is deprecated and should not be used for new applications.
12. Authorization Code Flow vs Client Credentials
Authorization Code
Used when a user is involved.
User β Client β Authorization Server β Resource Server
Example:
User logs in to a web application.
Client Credentials
Used when one service calls another service.
Service A β Authorization Server β Service B
Example:
Payment Service β Order Service
13. OAuth 2.0 and OpenID Connect
OAuth 2.0 alone does not define a standard way to identify the user.
OpenID Connect adds:
- ID Token.
- UserInfo Endpoint.
- Standard identity claims.
- Authentication flow.
Example
User logs in with Google
β
Google authenticates the user
β
Google returns ID Token
β
Application identifies the user
Example ID Token claims:
{
"iss": "https://accounts.google.com",
"sub": "123456789",
"aud": "client-id",
"email": "user@example.com",
"name": "John"
}
The sub claim is the unique identifier of the user at the identity provider.
14. Spring Security + OAuth 2.0
Spring Security supports OAuth 2.0 in different roles.
OAuth2 Client
Used when your application wants to access another service using OAuth 2.0.
Example:
Spring Boot Application
β
Google OAuth 2.0
β
Google API
OAuth2 Resource Server
Used when your application exposes protected APIs and validates access tokens.
Example:
Client
β
Bearer Token
β
Spring Boot API
β
Validate Token
β
Access Granted
OAuth2 Authorization Server
Used when your application acts as an authorization server.
Example:
Client Application
β
Your Authorization Server
β
Access Token
β
Protected API
15. Example β Login with Google
Imagine a Spring Boot application that allows users to log in with Google.
Architecture
+-------------------+
| User |
+---------+---------+
|
| 1. Click "Login with Google"
v
+---------+---------+
| Spring Boot App |
| OAuth2 Client |
+---------+---------+
|
| 2. Redirect
v
+---------+---------+
| Google OAuth 2.0 |
| Authorization |
| Server |
+---------+---------+
|
| 3. Login + Consent
|
| 4. Authorization Code
v
+---------+---------+
| Spring Boot App |
+---------+---------+
|
| 5. Exchange Code
v
+---------+---------+
| Google OAuth 2.0 |
+---------+---------+
|
| 6. ID Token + Access Token
v
+---------+---------+
| Spring Boot App |
+-------------------+
16. Implementing OAuth2 Login in Spring Boot
The following example uses Spring Boot and Spring Security to implement OAuth2 Login with Google.
16.1 Create the Project
Create a Spring Boot project with:
- Spring Web.
- Spring Security.
- OAuth2 Client.
Maven Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
17. Configure Google OAuth2 Client
First, create OAuth 2.0 credentials in Google Cloud Console.
You will need:
- Client ID.
- Client Secret.
- Redirect URI.
Example redirect URI:
http://localhost:8080/login/oauth2/code/google
The redirect URI must match the one configured in Google.
18. Configure application.properties
spring.security.oauth2.client.registration.google.client-id=YOUR_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_CLIENT_SECRET
spring.security.oauth2.client.registration.google.scope=openid,profile,email
Explanation
client-id
β
Identifies your application
client-secret
β
Authenticates your application
scope
β
Defines the requested permissions
Important
Do not commit the client secret to a public repository.
For production, use environment variables or a secrets manager.
19. Create the Security Configuration
Create a class called SecurityConfig.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
SecurityFilterChain securityFilterChain(
HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/error").permitAll()
.anyRequest().authenticated()
)
.oauth2Login(Customizer.withDefaults());
return http.build();
}
}
Explanation
@Configuration
β
Defines a Spring configuration class
@EnableWebSecurity
β
Enables Spring Security
authorizeHttpRequests()
β
Defines authorization rules
permitAll()
β
Allows public access
anyRequest().authenticated()
β
Requires authentication
oauth2Login()
β
Enables OAuth2 Login
20. Create a Controller
Create a controller with a protected endpoint.
@RestController
public class UserController {
@GetMapping("/")
public String home() {
return "Welcome to the application";
}
@GetMapping("/profile")
public String profile(Authentication authentication) {
return "Hello, " + authentication.getName();
}
}
Explanation
The /profile endpoint requires authentication.
If the user is not authenticated:
GET /profile
β
Spring Security
β
Redirect to Google Login
If the user is authenticated:
GET /profile
β
Spring Security
β
Controller
β
Hello, user
21. What Happens When the User Accesses /profile?
Step 1
The user accesses:
http://localhost:8080/profile
Step 2
Spring Security checks whether the user is authenticated.
Is user authenticated?
β
No
Step 3
Spring Security redirects the user to Google.
/oauth2/authorization/google
Step 4
Google authenticates the user.
Step 5
Google redirects the user back to:
/login/oauth2/code/google
Step 6
Spring Security exchanges the authorization code for tokens.
Step 7
Spring Security creates an authenticated session.
Step 8
The user is redirected to /profile.
Step 9
The controller returns:
Hello, user
22. OAuth2 Login Flow in Spring Security
User
|
| GET /profile
v
Spring Security
|
| User not authenticated
v
/oauth2/authorization/google
|
| Redirect
v
Google
|
| Login + Consent
v
Google
|
| Authorization Code
v
/login/oauth2/code/google
|
| Exchange Code
v
Google Token Endpoint
|
| ID Token + Access Token
v
Spring Security
|
| Create Authentication
v
SecurityContext
|
| Authenticated Request
v
Controller
|
| Response
v
User
23. Accessing User Information
After authentication, Spring Security provides the authenticated user.
Example:
@GetMapping("/user")
public Map<String, Object> user(
@AuthenticationPrincipal OAuth2User oauth2User) {
return oauth2User.getAttributes();
}
The OAuth2User contains attributes returned by the provider.
Example:
{
"sub": "123456789",
"name": "John Doe",
"email": "john@example.com",
"picture": "https://example.com/photo.jpg"
}
Important
The available attributes depend on the identity provider.
For example:
- Google provides
email,name, andpicture. - GitHub may provide
login,name, andavatar_url.
24. Example β Display the Authenticated User
@RestController
public class UserController {
@GetMapping("/user")
public Map<String, Object> user(
@AuthenticationPrincipal OAuth2User oauth2User) {
return Map.of(
"name", oauth2User.getAttribute("name"),
"email", oauth2User.getAttribute("email")
);
}
}
Example Response
{
"name": "John Doe",
"email": "john@example.com"
}
25. OAuth2 Client vs Resource Server
These are two different use cases.
OAuth2 Client
Your application logs in users through an external provider.
User
β
Google
β
Spring Boot Application
Example:
Login with Google
Resource Server
Your application receives access tokens and protects APIs.
Client
β
Access Token
β
Spring Boot API
Example:
GET /api/orders
Authorization: Bearer <token>
26. Example β Spring Boot as OAuth2 Resource Server
Now imagine a different application.
A frontend application obtains an access token from an authorization server and calls your Spring Boot API.
Architecture
+-------------------+
| Frontend App |
+---------+---------+
|
| Authorization: Bearer <token>
v
+---------+---------+
| Spring Boot API |
| Resource Server |
+---------+---------+
|
| Validate Token
v
+---------+---------+
| Authorization |
| Server |
+-------------------+
27. Add Resource Server Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
28. Configure JWT Validation
Example using an authorization server that exposes a JWK Set URI.
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://auth.example.com/oauth2/jwks
The resource server uses the public keys to validate JWT signatures.
Important
The exact configuration depends on the authorization server.
29. Configure Security for Resource Server
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
SecurityFilterChain securityFilterChain(
HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/public").permitAll()
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.jwt(Customizer.withDefaults())
);
return http.build();
}
}
Explanation
oauth2ResourceServer()
β
Configures the application as a Resource Server
jwt()
β
Configures JWT-based authentication
anyRequest().authenticated()
β
Requires a valid token
30. Create a Protected API
@RestController
@RequestMapping("/api")
public class OrderController {
@GetMapping("/orders")
public String orders() {
return "Protected orders";
}
}
Request
GET /api/orders
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...
Response
Protected orders
If the token is missing or invalid:
HTTP/1.1 401 Unauthorized
31. OAuth2 Resource Server Flow
Client
|
| 1. Sends Bearer Token
v
Spring Boot Resource Server
|
| 2. Extract Token
v
JWT Decoder
|
| 3. Validate Signature
|
| 4. Validate Claims
v
SecurityContext
|
| 5. Authenticated Request
v
Controller
|
| 6. Response
v
Client
32. OAuth2 + JWT
OAuth 2.0 defines how tokens are obtained and used.
JWT defines a token format.
Therefore:
OAuth 2.0
β
Authorization Framework
JWT
β
Token Format
OAuth 2.0 can use different token formats:
- JWT.
- Opaque tokens.
Example
OAuth 2.0 Authorization Server
β
Issues JWT Access Token
β
Spring Boot Resource Server
β
Validates JWT
33. JWT Access Token Example
A JWT contains three parts:
Header.Payload.Signature
Example:
eyJhbGciOiJSUzI1NiIs...
.
eyJzdWIiOiIxMjM0NTY3ODkiLCJzY29wZSI6InJlYWQifQ
.
signature
Example claims:
{
"iss": "https://auth.example.com",
"sub": "123456789",
"aud": "orders-api",
"scope": "orders.read",
"exp": 1735689600
}
Important Claims
iss
β
Issuer
sub
β
Subject
aud
β
Audience
scope
β
Permissions
exp
β
Expiration Time
34. OAuth2 Scopes and Spring Security Authorities
OAuth2 scopes can be converted into Spring Security authorities.
Example token:
{
"scope": "orders.read orders.write"
}
Spring Security may represent them as:
SCOPE_orders.read
SCOPE_orders.write
Example
@GetMapping("/orders")
@PreAuthorize("hasAuthority('SCOPE_orders.read')")
public String orders() {
return "Orders";
}
The user must have the orders.read scope.
35. Example β Role-Based Authorization with OAuth2
OAuth2 scopes and application roles are different concepts.
Scope
Defines what the client is allowed to access.
Example:
orders.read
Role
Defines what the user is allowed to do inside the application.
Example:
ROLE_ADMIN
Example
User
β
ROLE_ADMIN
β
Can access administrative resources
The authorization server may provide roles or groups as claims, but your application must configure how those claims map to Spring Security authorities.
36. OAuth2 Login vs Basic Authentication
Basic Authentication
Client
β
Username + Password
β
Spring Boot
OAuth2 Login
Client
β
Redirect to Identity Provider
β
User Authenticates
β
Authorization Code
β
Tokens
β
Spring Boot
Main Difference
Basic Authentication sends credentials directly to the application.
OAuth2 delegates authentication and authorization to an authorization server.
37. OAuth2 Login vs JWT Authentication
OAuth2 Login
Used when a user logs in through an external provider.
Example:
Login with Google
JWT Authentication
Used when a client sends a JWT to a protected API.
Example:
Authorization: Bearer <jwt>
They Can Work Together
User
β
Login with Google
β
Spring Boot OAuth2 Client
β
Application Session
β
Protected API
Or:
User
β
Login with Identity Provider
β
Access Token
β
Spring Boot Resource Server
β
Protected API
38. Common OAuth2 Endpoints
Authorization Endpoint
Used to start the authorization flow.
/oauth2/authorize
Token Endpoint
Used to exchange an authorization code for tokens.
/oauth2/token
UserInfo Endpoint
Used to retrieve user information in OpenID Connect.
/userinfo
JWK Set Endpoint
Used by resource servers to obtain public keys for JWT validation.
/oauth2/jwks
Revocation Endpoint
Used to revoke tokens.
/oauth2/revoke
Introspection Endpoint
Used to validate opaque tokens.
/oauth2/introspect
39. Common OAuth2 Errors
invalid_client
The client credentials are invalid.
Example:
Client ID or Client Secret is incorrect.
invalid_grant
The authorization code is invalid, expired, or already used.
invalid_scope
The requested scope is invalid or not allowed.
unauthorized_client
The client is not authorized to use the requested grant type.
access_denied
The user denied the authorization request.
40. OAuth2 Security Best Practices
Use HTTPS
OAuth2 tokens must be transmitted over HTTPS.
Use Authorization Code Flow
For user-based applications, use Authorization Code Flow with PKCE when appropriate.
Protect Client Secrets
Never expose client secrets in frontend applications.
Use Short-Lived Access Tokens
Access tokens should have limited lifetimes.
Validate Token Claims
Validate:
- Signature.
- Issuer.
- Audience.
- Expiration.
- Not-before time.
Use Least Privilege
Request only the scopes the application needs.
Protect Refresh Tokens
Refresh tokens must be stored securely.
Validate Redirect URIs
Use exact registered redirect URIs.
41. OAuth2 Authorization Code Flow with PKCE
PKCE stands for Proof Key for Code Exchange.
It adds protection against authorization code interception.
Flow
Client generates code_verifier
β
Client generates code_challenge
β
Client sends code_challenge
β
Authorization Server
β
Authorization Code
β
Client sends code_verifier
β
Authorization Server validates PKCE
β
Access Token
PKCE is especially important for public clients such as mobile applications and SPAs.
42. OAuth2 Example β Mobile Application
Imagine a mobile application that allows users to access their account.
Flow
Mobile App
β
Open System Browser
β
Identity Provider
β
User Login
β
Authorization Code
β
Mobile App
β
Access Token
β
API
The mobile application does not need to collect the userβs password.
43. OAuth2 Example β Machine-to-Machine
Imagine an Order Service calling an Inventory Service.
Flow
Order Service
β
Client Credentials
β
Authorization Server
β
Access Token
β
Inventory Service
β
Protected Resource
There is no user login.
The service authenticates using its own credentials.
44. OAuth2 Example β Single Sign-On
Imagine a company has multiple applications:
- HR Application.
- Payroll Application.
- Internal Portal.
All applications use the same identity provider.
Flow
User
β
Login to Internal Portal
β
Identity Provider
β
Authenticated
β
Access HR Application
β
Access Payroll Application
The user does not need to log in separately to every application.
This is the idea of Single Sign-On (SSO).
45. Important OAuth2 Terminology
| Term | Meaning |
|---|---|
| Resource Owner | Entity that owns the protected resources |
| Client | Application requesting access |
| Authorization Server | Issues tokens |
| Resource Server | Hosts protected resources |
| Access Token | Token used to access resources |
| Refresh Token | Token used to obtain a new access token |
| Authorization Code | Temporary code exchanged for tokens |
| Scope | Permission requested by the client |
| Grant Type | Method used to obtain a token |
| Redirect URI | URL where the authorization server redirects the user |
| ID Token | Token containing authentication information |
| PKCE | Protection mechanism for authorization code flow |
| JWT | Token format |
| OIDC | Authentication protocol built on OAuth 2.0 |
46. OAuth2 Interview Questions
What is OAuth 2.0?
OAuth 2.0 is an authorization framework that allows applications to access protected resources without sharing the userβs password.
What is the difference between OAuth2 and OpenID Connect?
OAuth2 is used for authorization. OpenID Connect adds authentication capabilities.
What is the difference between an access token and a refresh token?
An access token accesses protected resources. A refresh token obtains a new access token.
What is the difference between an authorization code and an access token?
An authorization code is temporary and exchanged for tokens. An access token is used to access protected resources.
What is the difference between OAuth2 Client and Resource Server?
An OAuth2 Client obtains tokens to access resources. A Resource Server validates tokens and protects APIs.
What is the difference between OAuth2 and JWT?
OAuth2 is an authorization framework. JWT is a token format.
What is PKCE?
PKCE protects the authorization code flow by requiring the client to prove that it initiated the authorization request.
What is the purpose of scopes?
Scopes define the permissions requested by the client.
What is the purpose of the redirect URI?
It defines where the authorization server sends the user after authorization.
47. Summary
OAuth 2.0 is an authorization framework that allows applications to access protected resources without sharing user passwords.
The main roles are:
Resource Owner
Client
Authorization Server
Resource Server
The most common user-based flow is:
Authorization Code Flow
The main tokens are:
Access Token
Refresh Token
For authentication, OAuth2 is commonly combined with:
OpenID Connect
In Spring Security, OAuth2 can be used to:
- Implement Login with Google.
- Implement Single Sign-On.
- Access external APIs.
- Protect REST APIs.
- Validate JWT access tokens.
- Implement machine-to-machine authentication.
The most important distinction is:
OAuth2 Client
β
Obtains tokens
OAuth2 Resource Server
β
Validates tokens
OAuth2 Authorization Server
β
Issues tokens
Understanding these three roles is essential for designing secure Spring Boot applications.
48. Practical Exercise
Implement a Spring Boot application with the following requirements:
Part 1 β OAuth2 Login
- Configure Google OAuth2.
- Create a protected
/profileendpoint. - Display the authenticated userβs name and email.
- Allow public access to
/.
Part 2 β Resource Server
- Configure JWT validation.
- Create a protected
/api/ordersendpoint. - Require authentication.
- Return
401 Unauthorizedwhen the token is missing or invalid.
Part 3 β Authorization
- Create a scope called
orders.read. - Allow only users with
SCOPE_orders.readto access/api/orders.
Part 4 β Questions
- What is the difference between OAuth2 and OpenID Connect?
- What is the difference between an access token and a refresh token?
- What is the role of the authorization server?
- What is the role of the resource server?
- What is the difference between OAuth2 Client and Resource Server?
- What is PKCE?
- What is the purpose of scopes?
- Why should client secrets never be exposed in frontend applications?