Skip to main content

How to Check a JWT's Issuer and Audience in Java with JJWT

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

JJWT checks a token's issuer and audience when you call requireIssuer and requireAudience on its parser builder. A wrong value throws IncorrectClaimException, and a missing claim throws MissingClaimException, so your code can tell the two apart.

Syntax​

Syntax
Claims claims = Jwts.parser()
.verifyWith(<key>)
.requireIssuer("<expected iss>")
.requireAudience("<expected aud>")
.build()
.parseSignedClaims(<token>)
.getPayload();
PartWhat it does
verifyWith(<key>)The key that checks the signature.
requireIssuerThe iss the token must have.
requireAudienceAn audience the token's aud must contain, usually your own API's name.
parseSignedClaims(<token>)Verifies the token and runs the checks. It throws when any of them fails.

The JJWT README, Claim Assertions: "You can enforce that the JWT you are parsing conforms to expectations that you require and are important for your application." It adds that you can catch "either MissingClaimException or IncorrectClaimException" to tell a missing claim from a wrong one.

The Example​

The example builds two tokens issued by https://login.example.org: one for https://photos.example.org, and one with no audience at all. It parses them four times: the first token with the right issuer and audience, with the audience of another API and with another issuer, and the second token with the right values.

Prerequisites​

  • A JDK and Maven. This guide uses OpenJDK 25.0.5.1 and Maven 3.10.2.

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:

pom.xml
<?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:

Main.java, which defines check(token, issuer, audience) with requireIssuer and requireAudience, catching MissingClaimException and IncorrectClaimException separately, then checks a token for the photos API three ways and a token without an audience once.

src/main/java/com/example/Main.java
package com.example;

import io.jsonwebtoken.IncorrectClaimException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MissingClaimException;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import javax.crypto.SecretKey;

public class Main {

// 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.
static final String SECRET = "C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64=";
static final SecretKey KEY = Keys.hmacShaKeyFor(Decoders.BASE64.decode(SECRET));

static void check(String token, String issuer, String audience) {
try {
String subject = Jwts.parser()
.verifyWith(KEY)
.requireIssuer(issuer)
.requireAudience(audience)
.build()
.parseSignedClaims(token)
.getPayload()
.getSubject();
System.out.println("accepted: sub " + subject);
} catch (MissingClaimException e) {
System.out.println("missing: " + e.getMessage());
} catch (IncorrectClaimException e) {
System.out.println("incorrect: " + e.getMessage());
}
}

public static void main(String[] args) {
Date expires = Date.from(Instant.now().plus(Duration.ofHours(2)));
String token = Jwts.builder()
.issuer("https://login.example.org")
.subject("ada")
.audience().add("https://photos.example.org").and()
.expiration(expires)
.signWith(KEY)
.compact();
String noAudience = Jwts.builder()
.issuer("https://login.example.org")
.subject("ada")
.expiration(expires)
.signWith(KEY)
.compact();

check(token, "https://login.example.org", "https://photos.example.org");
check(token, "https://login.example.org", "https://videos.example.org");
check(token, "https://other.example.org", "https://photos.example.org");
check(noAudience, "https://login.example.org", "https://photos.example.org");
}
}

check catches MissingClaimException and IncorrectClaimException separately, so the output says which kind of problem each token had.

Step 3: Run It​

Terminal
cd demo
mvn -q compile exec:java
Output
accepted: sub ada
incorrect: Missing expected 'https://videos.example.org' value in 'aud' claim [https://photos.example.org].
incorrect: Expected iss claim to be: https://other.example.org, but was: https://login.example.org.
missing: Missing 'aud' claim. Expected values: [https://photos.example.org]

A zsh terminal where mvn compile exec prints accepted sub ada, then incorrect for the videos audience and for another issuer, then missing for the token without an aud claim.

What Happened​

  • The right issuer and audience passed.
  • The videos audience was incorrect: JJWT looked for https://videos.example.org in the token's aud set and didn't find it.
  • Another issuer was incorrect too, and the message shows both the expected issuer and the one in the token.
  • The token without aud was missing it. requireAudience makes the claim required, not just checked when it's there.

Without requireAudience, JJWT doesn't look at aud at all, so a token made for any API would pass.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out requireAudienceJJWT ignores aud, and a token made for another API passesRequire your API's audience
Catching only JwtExceptionYou can't tell a missing claim from a wrong oneCatch MissingClaimException and IncorrectClaimException first when it matters
Using the general require("aud", ...)It compares the whole value, and JJWT reads aud as a set, so even the right audience fails: Expected aud claim to be: https://photos.example.org, but was: [https://photos.example.org].Use requireAudience

Troubleshooting​

SymptomLikely causeFix
IncorrectClaimException: Missing expected '...' value in 'aud' claim [...]The token's aud doesn't contain your audienceCheck which API the token was made for
IncorrectClaimException: Expected iss claim to be: ..., but was: ...The token comes from another issuerUse the issuer exactly as the token has it
MissingClaimException: Missing 'aud' claim. Expected values: [...]The token has no audHave the sign-in service set an audience

FAQ​

Can I accept several issuers?​

requireIssuer takes one value. To trust several issuers, parse without it and check claims.getIssuer() against your own list.

Does requireAudience accept a token with several audiences?​

Yes, when your audience is one of them: JJWT looks for the expected value inside the token's aud set.

Conclusion​

Call requireIssuer and requireAudience on the parser builder, and JJWT rejects tokens from other issuers, tokens made for other APIs and tokens without the claims, with an exception type that tells you which.

Additional Resources​