Home

How to Generate JSON Web Tokens (JWT) using Spring Boot

In modern web development, securing APIs is crucial. One of the most popular standards for authentication is the JSON Web Token (JWT).

In this tutorial, we will learn how to generate a secure JWT using Spring Boot. Unlike standard tutorials that often use email addresses, we will build a system that authenticates using a Phone Number and includes custom Roles inside the token.

Tutorial Description

We will build a simple REST API endpoint. When a user sends their phone number and roles, our application will sign this data using a secret key and return a JWT string. This token can later be used by the client to access protected resources.

Tools and Technologies Used

  • Language: Java 21 (Compatible with Java 17+)
  • Framework: Spring Boot 3.2.x
  • Build Tool: Maven
  • JWT Library: Auth0 java-jwt (Version 4.4.0)
  • Testing Tool: Postman
  • IDE: IntelliJ IDEA or Eclipse

Development Steps

Step 1: Project Setup and Dependencies
Step 2: Configure Application Properties
Step 3: Create the Request DTO
Step 4: Create the JWT Service
Step 5: Create the Controller
Step 6: How to Test This?

1. 1. Step 1: Project Setup and Dependencies

First, create a new Spring Boot project. If you are using Spring Initializr, select "Spring Web".

Once created, open your pom.xml file. We need to add the Auth0 Java JWT library to handle the token generation.

1. pom.xml

 

Code Example

2. Step 2: Configure Application Properties

We need to define a Secret Key (used to sign the token so it cannot be tampered with) and an Expiration Time (how long the token is valid).

1. File: src/main/resources/application.properties

 

Code Example

3. Step 3: Create the Request DTO

We need a simple Java class to represent the data coming from the user (the phone number and their roles). This is often called a DTO (Data Transfer Object).

1. File: src/main/java/com/planetlearning/jwt/dto/TokenRequest.java

Code Example

4. Step 4: Create the JWT Service

This is the core logic of the tutorial. We will create a service that takes the phone number and roles, sets the expiration time, and signs the token using the algorithm.

1. File: src/main/java/com/planetlearning/jwt/service/JwtService.java

 

Code Example

5. Step 5: Create the Controller

Finally, we create a REST Controller to expose an endpoint. This allows users to send a POST request to generate their token.

1. File: src/main/java/com/planetlearning/jwt/controller/AuthController.java

Code Example

6. How to Test This?

Now that the code is ready, run your Spring Boot application. We will use Postman to test the API.

  1. Open Postman.
  2. Create a new request tab.
  3. Set the HTTP Method to POST.
  4. Set the URL to: http://localhost:8080/api/auth/generate.
  5. Click on the Body tab, select raw, and choose JSON from the dropdown.
  6. Paste the following JSON:
  7. {
        "phoneNumber": "+1234567890",
        "roles": ["STUDENT", "PREMIUM_MEMBER"]
    }
  8. Click Send
  9. Expected Output

    You should receive a 200 OK status code with a generated token:

    {
     "access_token": " eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIrMTIzNDU2Nzg5MCIsInJvbGVzIjpbIlNUVURFTlQiLCJQUkVNSVVNX01FTUJFUiJdLCJpYXQiOjE3..."
    }

Verifying the Token

To confirm the token actually contains the phone number and roles:

  1. Copy the access_token string you just received.
  2. Go to jwt.io.
  3. Paste the token into the "Encoded" box.
  4. Look at the Payload section on the right. You should see:
    • sub: "+1234567890"
    • roles: ["STUDENT", "PREMIUM_MEMBER"]

Congratulations! You have successfully implemented JWT generation using Spring Boot with custom claims.