Introduction to OAuth 2.0 and JWT in Java
Modern web applications and APIs require reliable authentication and authorization mechanisms. The two most popular standards in this area are OAuth 2.0 and JWT (JSON Web Tokens). If you are a beginner Java developer, understanding these technologies will open the door to creating secure and scalable applications.
In this article, we will break down what OAuth 2.0 and JWT are, how they work together, and how to implement them in Java using popular libraries. You will learn how to set up an authorization server, generate and validate JWT tokens, and protect your REST APIs.
What are OAuth 2.0 and JWT?
OAuth 2.0 — Delegated Access Protocol
OAuth 2.0 is an authorization protocol that allows applications to obtain limited access to user resources without sharing their credentials. Imagine you want to give an application access to your photos in Google. Instead of telling the application your Google password, you use OAuth 2.0, which issues the application a temporary token with limited permissions.
The main participants in OAuth 2.0:
- Resource Owner — the user who owns the resources
- Client — the application requesting access
- Authorization Server — the server that issues tokens
- Resource Server — the server that provides protected resources
JWT — Compact Token Format
JWT (JSON Web Token) is an open standard (RFC 7519) for creating access tokens that contain claims in JSON format. JWT consists of three parts separated by dots:
- Header — contains the token type and signing algorithm (e.g., HS256 or RS256)
- Payload — contains claims (e.g., user ID, roles, expiration time)
- Signature — a signature that guarantees the token's integrity
Example JWT: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U
Implementing OAuth 2.0 and JWT in Java
Setting Up the Project with Spring Security
To implement OAuth 2.0 and JWT in Java, we will use Spring Boot and Spring Security. Let's start by adding dependencies to the pom.xml file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>0.12.3</version></dependency><dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>0.12.3</version> <scope>runtime</scope></dependency><dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>0.12.3</version> <scope>runtime</scope></dependency>Creating a JWT Provider
Let's create a JwtProvider class that will be responsible for generating and validating JWT tokens:
import io.jsonwebtoken.*;import io.jsonwebtoken.security.Keys;import org.springframework.stereotype.Component;
import javax.crypto.SecretKey;import java.util.Date;
@Componentpublic class JwtProvider { private final SecretKey secretKey = Keys.secretKeyFor(SignatureAlgorithm.HS256); private final