Jwts.parserBuilder() no longer compiles after upgrading JJWT
Compile error on Jwts.parserBuilder() after a version bumpShort answer
JJWT 0.12.0 removed Jwts.parserBuilder() because Jwts.parser() now returns the builder itself. Replace the chain with Jwts.parser().verifyWith(key).build().parseSignedClaims(token).getPayload(), and declare all three JJWT artifacts on one version so parsing also works at runtime.
Fix it fast
1Find out which cause you have
Find out which JJWT version the build really resolves. Code written for 0.11 and a 0.12+ jar on the classpath is the usual mismatch.
./mvnw dependency:tree -Dincludes=io.jsonwebtoken # Gradle ./gradlew dependencies --configuration runtimeClasspath | grep jsonwebtoken2Match what you see, then jump to its fix
- 1
A compile error on parserBuilder() right after a version bumpThe method was removed in 0.12.0Fix - 2
Deprecation warnings or errors on parseClaimsJws, getBody, Keys.secretKeyForOld method names such as parseClaimsJws, getBody and setSigningKey are still in the codeFix - 3
Code compiles, but parsing fails at runtimeOnly the API artifact is on the classpathFix
- 1
What does the error look like?
Compile error on Jwts.parserBuilder() after a version bumpsetSigningKey / parseClaimsJws / getBody shown as deprecated
Cause 1 of 3
The method was removed in 0.12.0
1Check
The 0.12.0 release is the project's first breaking change release, and its notes say parserBuilder() was removed as redundant.
grep -rn "parserBuilder" src/2Fix
Call Jwts.parser() instead. It now returns a JwtParserBuilder, so set the key on it and then call build().
Claims claims = Jwts.parser()
.verifyWith(secretKey)
.build()
.parseSignedClaims(token)
.getPayload();3Verify
Compile the project.
./mvnw -q compileConfirmed by raw.githubusercontent.com
Cause 2 of 3
Old method names such as parseClaimsJws, getBody and setSigningKey are still in the code
1Check
Search for the 0.11 style calls. The 0.12.0 notes deprecate parseClaimsJws in favour of parseSignedClaims, and getBody in favour of getPayload.
grep -rnE "parseClaimsJws|getBody\(|secretKeyFor|setSigningKey" src/2Fix
Use parseSignedClaims and getPayload. To create a new HMAC key use the algorithm's key builder instead of Keys.secretKeyFor.
SecretKey key = Jwts.SIG.HS256.key().build();3Verify
Recompile and run the tests that parse a token.
./mvnw -q testConfirmed by raw.githubusercontent.com
Cause 3 of 3
Only the API artifact is on the classpath
1Check
JJWT is split in three artifacts. jjwt-api is compile scope; jjwt-impl and a JSON module are needed at runtime.
grep -n "jjwt" pom.xml2Fix
Declare all three, on the same version.
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.13.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.13.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.13.0</version>
<scope>runtime</scope>
</dependency>3Verify
Run the app and parse one token.
./mvnw -q testConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Pin all three JJWT artifacts to one version through a property or a BOM.
- Read the project's CHANGELOG before bumping a security library.
- Cover token parsing with a unit test so an upgrade breaks CI, not production.
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?
Projects of mine built on the same technologies:
Related fixes
Security
JJWT rejects a token because the signature does not match
Login works but every request with the issued token is rejected
JJWT recomputes the signature with the key you give the parser.
3 causes, each with a fixSecurity
Spring Boot cannot find a ClientRegistrationRepository bean for OAuth2 login
APPLICATION FAILED TO START
Boot creates the ClientRegistrationRepository from spring.security.oauth2.client.registration properties.
3 causes, each with a fixSecurity
Spring Security fails to resolve the issuer configuration for a JWT or Keycloak
The identity server is up but the Spring app fails on the first token
Spring Security fetches the issuer's discovery document before it can validate tokens.
3 causes, each with a fixSecurity
Spring Security cannot verify the CSRF token because the session is gone
InvalidCsrfTokenException
By default Spring Security keeps the expected CSRF token in the HTTP session.
3 causes, each with a fixSecurity
POST requests return 403 in Spring Security because the CSRF token is missing
MissingCsrfTokenException
Spring Security checks a CSRF token on every request except GET, HEAD, TRACE and OPTIONS.
4 causes, each with a fixSecurity
Changing the error response when a JWT is rejected in Spring Security
403 Forbidden for a valid token that lacks the required authority
A resource server answers a bad token with 401 and a WWW-Authenticate header, and a valid token without enough rights with 403.
2 causes, each with a fix