Fix JWT validation in Java and Spring Boot
==========================================

Build the JwtDecoder bean with
NimbusJwtDecoder.withPublicKey(key).signatureAlgorithm(RS256) (or
withIssuerLocation for rotating keys) and set its validators:
JwtValidators.createDefaultWithValidators(timestamps, new
JwtIssuerValidator(issuer), new JwtAudienceValidator(audience)), with a
JwtTimestampValidator that requires exp. The default validators check neither
issuer nor audience.

Page: https://softwaresecurity.com/fix/jwt-validation/java-spring/

What is in this archive
-----------------------
jwt-validation/java-spring/   the project: three implementations of the same code and one test suite
  src/main/java/com/example/accounts/impl/VulnerableTokenDecoder.java   (vulnerable)
  src/main/java/com/example/accounts/impl/FixedTokenDecoder.java   (fixed)
  src/main/java/com/example/accounts/impl/WrongTokenDecoder.java   (a plausible fix that still fails)
run-all.mjs   runs the tests in all three modes and checks the results (Node.js only)
LICENSE-CODE.txt

Run it
------
  Tested with OpenJDK 21.0.12.1 (Homebrew), Maven 3.9.16. run-all.mjs needs
  Node.js 22 or later. Supported versions:
  https://softwaresecurity.com/fix/versions/

  From the jwt-validation/java-spring folder, install the dependencies once
  (this needs network access):

    mvn -B -q test

  Then run the tests. IMPL chooses the implementation the tests load:

    # every test passes
    IMPL=fixed mvn -B -o -q test
    # an exploit test fails
    IMPL=vulnerable mvn -B -o -q test
    # a test fails
    IMPL=wrong mvn -B -o -q test

  The JUnit report is written to target/surefire-reports/TEST-*.xml. To run all
  three modes and check the results in one step, run the harness from this top
  folder (it needs only Node.js; --install runs the install step first):

    node run-all.mjs --install jwt-validation/java-spring
    node run-all.mjs jwt-validation/java-spring

Verified
--------
  11 October 2026 on macOS 27.0 (Darwin 27.0.0, arm64), with java OpenJDK
  21.0.12.1 (Homebrew), maven 3.9.16, springBoot 4.1.1, springSecurity 7.1.1,
  nimbusJoseJwt 10.9.1, tomcat 11.0.26, junitJupiter 6.0.3. Versions and results
  hold for that date: run the tests with your own versions before relying on the
  fix.

Licence
-------
  The code is MIT licensed (LICENSE-CODE.txt). The page text is CC BY 4.0.
