Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Security and hardening

JJWT 0.12 marks setClaims, setSubject and signWith(SignatureAlgorithm) as deprecated when you build a token

Your error

jjwt: setClaims(java.util.Map) is deprecated2 more lines it can show

Fix it fast

Most common cause, 1 of 4

setClaims or addClaims still fills the payload

Use claims(Map), which adds the pairs and overwrites only names that already exist.

Fix
// add a map to whatever is already set
String jws = Jwts.builder()
    .subject(userId)
    .claims(extraClaims)
    .signWith(key)
    .compact();

// replace everything set so far, as the old setClaims did
String replaced = Jwts.builder()
    .claims().empty().add(extraClaims).and()
    .signWith(key)
    .compact();
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

In JJWT 0.12 and later, JwtBuilder.setClaims(Map) is deprecated in favour of claims(Map) or the claims() builder, and setSubject, setExpiration and signWith(SignatureAlgorithm, key) went the same way. setClaims replaced the whole payload while claims(Map) adds to it. Rename the calls to subject(), expiration(), issuedAt(), sign with signWith(key) or Jwts.SIG, and build standalone claims with Jwts.claims().build().

  • Applies to JJWT 0.13.0 (jjwt-api, jjwt-impl, jjwt-jackson), JJWT 0.12.x, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List every token-building call that 0.12 changed. Each pattern in the search belongs to one cause below, so the matches show which edits the code needs.

Run this first
grep -rnE "setClaims\(|addClaims\(|\.set(Subject|Issuer|Audience|Expiration|NotBefore|IssuedAt|Id)\(|signWith\(SignatureAlgorithm|SignatureAlgorithm\.|Jwts\.claims\(\)" src/

Then find the line you see in this list and open its fix:

  1. 1setClaims or addClaims still fills the payloadMatches for setClaims( or addClaims(, and sometimes a token that lacks a claim you set a line earlier
  2. 2The standard claim setters use the old set prefixWarnings point at setSubject, setIssuer, setExpiration or setIssuedAt rather than setClaims
  3. 3signWith still takes the SignatureAlgorithm enumMatches for signWith(SignatureAlgorithm.HS256, ...) or signWith(key, SignatureAlgorithm.RS256)
  4. 4Jwts.claims() now returns a builder, not a Claims objectA compile error on a line such as Claims claims = Jwts.claims(); after the upgrade, not just a warning

What does the error look like?

  • jjwt: setClaims(java.util.Map) is deprecated
  • Deprecation warnings on setSubject, setExpiration and signWith(SignatureAlgorithm, ...) after moving to JJWT 0.12
  • The token built with setClaims has no sub claim even though setSubject was called

Cause 1 of 4

setClaims or addClaims still fills the payload

You seeMatches for setClaims( or addClaims(, and sometimes a token that lacks a claim you set a line earlier

Check

JwtBuilder.setClaims(Map) is deprecated since 0.12.0, and its Javadoc describes it as a replace: it empties the claims and then adds the map. Code that called setSubject before setClaims therefore lost the subject. addClaims is deprecated too and will be removed before 1.0.

Fix

Use claims(Map), which adds the pairs and overwrites only names that already exist. When you really mean to wipe earlier claims, say so with the nested builder.

Fix
// add a map to whatever is already set
String jws = Jwts.builder()
    .subject(userId)
    .claims(extraClaims)
    .signWith(key)
    .compact();

// replace everything set so far, as the old setClaims did
String replaced = Jwts.builder()
    .claims().empty().add(extraClaims).and()
    .signWith(key)
    .compact();

Verify

Decode the new token's payload and confirm sub and every custom claim are present, and recompile with no deprecation warning for setClaims.

Verify
./mvnw -q compile

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The standard claim setters use the old set prefix

You seeWarnings point at setSubject, setIssuer, setExpiration or setIssuedAt rather than setClaims

Check

ClaimsMutator, which JwtBuilder implements, deprecates each setter since 0.12.0 in favour of a shorter builder-style name. setAudience is replaced by the audience() collection because aud may hold several values.

Fix

Drop the set prefix. For the audience, add values through the nested collection and return to the builder with and().

Fix
String jws = Jwts.builder()
    .issuer("https://auth.example.com")
    .subject(userId)
    .audience().add("orders-api").and()
    .issuedAt(now)
    .expiration(new Date(now.getTime() + 15 * 60 * 1000))
    .signWith(key)
    .compact();

Verify

The build compiles cleanly and the decoded payload holds iss, sub, aud, iat and exp with the values you passed.

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

signWith still takes the SignatureAlgorithm enum

You seeMatches for signWith(SignatureAlgorithm.HS256, ...) or signWith(key, SignatureAlgorithm.RS256)

Check

The changelog for 0.12.0 deprecates the io.jsonwebtoken.SignatureAlgorithm enum in favour of the Jwts.SIG registry, and JwtBuilder deprecates every signWith overload that accepts the enum. Keys.secretKeyFor, which also takes the enum, is deprecated with it.

Fix

Call signWith(key), which picks the algorithm from the key's type and length, or name one through Jwts.SIG. Generate new HMAC keys from the algorithm itself.

Fix
SecretKey key = Jwts.SIG.HS256.key().build();
String jws = Jwts.builder().subject(userId).signWith(key, Jwts.SIG.HS256).compact();

Verify

No warning mentions SignatureAlgorithm, and the token header still shows the alg your verifiers expect.

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

Jwts.claims() now returns a builder, not a Claims object

You seeA compile error on a line such as Claims claims = Jwts.claims(); after the upgrade, not just a warning

Check

Claims and Header objects became immutable in 0.12.0. The changelog lists, among the breaking changes, that Jwts.claims() returns a ClaimsBuilder instead of a Claims instance, so code that created an empty Claims and filled it no longer compiles.

Fix

Finish the builder with build(), or skip the separate object and pass the values straight to the JwtBuilder.

Fix
Claims claims = Jwts.claims()
    .subject(userId)
    .add("role", "admin")
    .build();

Verify

The class compiles and the token carries the claims taken from the built object.

Verify
./mvnw -q test

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Compile with deprecation warnings visible and treat new ones in the security package as a task, because JJWT says some of them will go before 1.0.
  • Keep a unit test that builds a token and asserts each expected claim, which catches the replace-versus-add change straight away.
  • Move jjwt-api, jjwt-impl and the JSON module together through one version property.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.