Spring Boot services · Chapter 26 of 41

JWT fundamentals

By the end of this chapter you can issue a signed JWT, check one (signature and expiry), and explain what anyone can read inside it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Security
  • 6 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

HTTP Basic works, but Anjali has read the app's network log: every request carries Ravi's password. If one log file leaks, passwords leak. And every request makes the server run BCrypt again, which is slow on purpose.

With what you know so far, the only way to say who is calling is to send the password each time, or to keep a session on the server, which a fleet of counter terminals and partner servers does not want. The answer is a ticket: log in once, get a signed token, show the token after that. This chapter only makes and checks tokens; the next one puts them on the API.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    JwtService.java: issue a signed token with the subject and roles

    Write JwtService.issue(subject, roles) so it returns a compact JWT (header.payload.signature) signed with the service's key, with the subject as sub and the roles, in order, as a list claim named roles. issue("meera@tapri.in", ["ADMIN", "MANAGER"]) gives a token whose roles claim is ["ADMIN", "MANAGER"].

  2. Task 2

    JwtService.java: issued now, expiring after the ttl

    Make the token issued by issue carry iat (issued at) equal to the injected Clock's current instant, and exp (expiration) equal to that instant plus the configured ttl: with the clock at 06:00 and a 15 minute ttl, exp is 06:15; with a 5 minute ttl, 06:05.

    Take the time from the Clock field, never from the system time.

  3. Task 3

    JwtService.java: verify a token and return its claims

    Write parse(token) so it verifies a token signed with the service's key and returns its claims (subject, roles), judging expiry by the injected Clock: a token issued at 06:00 with a 10 minute life, parsed when the clock says 06:01, gives sub anjali@tapri.in and roles ["MANAGER"].

    Write isValid(token) so it answers true for a token parse accepts, and false (never an exception) otherwise.

  4. Task 4

    JwtService.java: reject a tampered token and another key's token

    Make parse throw a JwtException, and isValid answer false, for a token whose payload was changed (CUSTOMER edited to ADMIN) and for a token signed with another key. isValid("not-a-token") also answers false.

  5. Task 5

    JwtService.java: reject an expired token, by the injected clock

    Judge expiry by the injected Clock: a token issued at 06:00 with a 15 minute life is valid when the clock says 06:14, and at 06:16 isValid answers false and parse throws ExpiredJwtException.

  6. Task 6 · interview question

    Interview: read a JWT's payload without the key

    Write the static decodePayload(token) so it returns the token's payload as a JSON string, read without any key, for a token signed with any key: {"sub":"ravi@tapri.in","roles":["CUSTOMER"],"iat":...,"exp":...}, starting with { and ending with }.

    Be ready to explain what anyone can read in a JWT, and what the key protects.

What you will learn

  • A JWT's three parts: header, payload (the claims) and signature.
  • Signing and checking with jjwt: who can make a token, and what "valid" means.
  • Expiry and the clock: why the parser must use your Clock.
  • What a JWT hides: nothing (the interview question).

More in Security

Build "JWT fundamentals" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.