How to Add Registered Claims to a JWT in Java with JJWT
JJWT's builder has a method for each of the seven registered JWT claims: issuer, subject, audience, issuedAt, notBefore, expiration and id, which writes jti. The audience is the one that works differently: audience().add(...) writes an array, even for a single audience.
Syntax
String token = Jwts.builder()
.issuer("<iss>")
.subject("<sub>")
.audience().add("<aud>").and()
.issuedAt(<Date>)
.notBefore(<Date>)
.expiration(<Date>)
.id("<jti>")
.signWith(<key>)
.compact();
| Part | What it does |
|---|---|
issuer, subject, id | Write iss, sub and jti. |
audience() | Opens a small builder for aud. add adds one audience, and and() goes back to the token. |
issuedAt, notBefore, expiration | Write iat, nbf and exp from java.util.Date values. |
signWith(<key>).compact() | Signs the token and returns it as text. |
The JJWT README, Standard Claims: "The JwtBuilder provides convenient builder methods for standard registered Claim names defined in the JWT specification."
The Example
The example builds a token for a user named ada, issued by https://login.example.org for https://photos.example.org, with iat and nbf set to now, exp two hours later and a random UUID as its ID. Then it prints the token's payload, decoded from its middle piece.
Prerequisites
Step 1: Create the Project
Create a folder named demo with this pom.xml. JJWT comes in three parts: jjwt-api for your code, and jjwt-impl and jjwt-jackson, which do the work while the program runs:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jjwt.version>0.13.0</jjwt.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- jjwt-jackson asks for Jackson 2.12.7.1. This makes Maven use the newest Jackson 2 instead. -->
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.23.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 2: Write the Code
Create src/main/java/com/example/Main.java:

package com.example;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.UUID;
import javax.crypto.SecretKey;
public class Main {
public static void main(String[] args) {
// The signing key: 32 random bytes, written as base64.
// It's in the code only for this example; a real app loads it from its configuration.
byte[] secret = Decoders.BASE64.decode("C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64=");
SecretKey key = Keys.hmacShaKeyFor(secret);
Instant now = Instant.now();
String token = Jwts.builder()
.issuer("https://login.example.org")
.subject("ada")
.audience().add("https://photos.example.org").and()
.issuedAt(Date.from(now))
.notBefore(Date.from(now))
.expiration(Date.from(now.plus(Duration.ofHours(2))))
.id(UUID.randomUUID().toString())
.signWith(key)
.compact();
System.out.println(token);
String payload = token.split("\\.")[1];
System.out.println(new String(Decoders.BASE64URL.decode(payload), StandardCharsets.UTF_8));
}
}
Date.from(now) turns an Instant into the Date JJWT expects. The last two lines take the token's middle piece and decode it with JJWT's Decoders.BASE64URL, to show the claims as JSON.
Step 3: Run It
cd demo
mvn -q compile exec:java
eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2xvZ2luLmV4YW1wbGUub3JnIiwic3ViIjoiYWRhIiwiYXVkIjpbImh0dHBzOi8vcGhvdG9zLmV4YW1wbGUub3JnIl0sImlhdCI6MTc5NjA1OTU4NSwibmJmIjoxNzk2MDU5NTg1LCJleHAiOjE3OTYwNjY3ODUsImp0aSI6IjU1NWQyZDA2LTVhZWYtNDhhZC1iMjY5LWY3MGM2ZWY4YzMyMSJ9.xrdhaDnsgPojOIQnl9jzcg4v2cCPvZ042UmY62gFkdk
{"iss":"https://login.example.org","sub":"ada","aud":["https://photos.example.org"],"iat":1796059585,"nbf":1796059585,"exp":1796066785,"jti":"555d2d06-5aef-48ad-b269-f70c6ef8c321"}

What Happened
- All seven claims are in the payload, in the order the methods were called.
audis an array:["https://photos.example.org"], even though the token has one audience. The JWT standard allows both forms.expis 7200 higher thaniat: two hours in seconds. JJWT drops the milliseconds of eachDate.
To write a single audience as a plain string, use audience().single("https://photos.example.org") instead of add(...).and(). It returns to the builder by itself, and the payload then held "aud":"https://photos.example.org".
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Forgetting .and() after audience().add(...) | The next builder method isn't found | End the audience with .and() |
Expecting a string aud | Services that read aud as one string fail on the array | Use audience().single(...) when a reader needs a string |
Leaving jjwt-impl out of the POM | The code compiles, then fails at run time with Unable to load class named [io.jsonwebtoken.impl.security.KeysBridge] | Keep all three JJWT dependencies |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
cannot find symbol with symbol: method issuedAt(java.util.Date), or another claim method | It was called on the audience builder, before .and() | Add .and() after the audiences |
ExceptionInInitializerError: Unable to load class named [io.jsonwebtoken.impl.security.KeysBridge] | jjwt-impl isn't on the runtime classpath | Add it with runtime scope |
FAQ
How do I add several audiences?
Call add once for each, before .and(): .audience().add("https://photos.example.org").add("https://videos.example.org").and().
Do I have to set all seven claims?
No. The JWT standard makes every registered claim optional. Set the ones your API checks, and always an expiry.
Conclusion
JJWT's builder writes each registered claim with its own method. Remember that audience().add(...) writes an array, and audience().single(...) writes a string.
Additional Resources
- Standard Claims, in the JJWT README.
- RFC 7519 section 4.1, the registered claim names.
