liveBook · Manning

JSON Web Token

As explained in Spring Security in Action, Second Edition

A JSON Web Token is a compact token format that represents claims as a digitally signed or otherwise protected JSON-based value.

A JSON Web Token (JWT) is a compact, non-opaque access-token format. It carries client or user claims so a resource server can authenticate the token and apply authorization without first requesting token details from the authorization server. section 13.2 section 13.2.2

Figure 13.8 The anatomy of a JWT token. The header and the body contain the details needed for the resource server to validate the token’s authenticity and apply the authorization constraints.

Use the token anatomy to distinguish the header, JSON body, and signature that make up a JWT. section 13.2.2

A JWT contains three dot-separated, Base64-encoded parts:

  • Header — usually identifies the signing algorithm and the authorization server’s key ID. section 13.2.2
  • Body — contains JSON claims about the client and user to whom the token was issued. section 13.2.2
  • Signature — provides cryptographic evidence that the expected authorization server issued the token and that its header or body was not altered. section 13.2.2

The header and body are encoded for transfer, not hidden from whoever obtains the token. Keep the carried data limited, especially when it is large or sensitive; use an opaque token when those concerns require introspection instead. section 13.2.2

In an OAuth 2 system, the authorization server issues the JWT and the client presents it to a resource server. The resource server uses the claims to enforce access rules. section 13.6 section 13.1

Validate JWTs in a Spring resource server

Add the Spring Boot resource-server, security, and web dependencies; expose a test endpoint such as /demo; configure the authorization server’s public key set URI; then require authentication. A SecurityFilterChain can use oauth2ResourceServer(c -> c.jwt(j -> j.jwkSetUri(keySetUri))) and authorizeHttpRequests(c -> c.anyRequest().authenticated()). The resource server retrieves public keys from the authorization server and uses them to verify access-token signatures. section 15.1 section 15.5

Figure 15.2 The resource server retrieves a set of public keys from the authorization server through an endpoint made available by the authorization server. The resource server then uses these keys to verify the signature of the access token.

Follow the key-distribution flow: the resource server obtains public key material from the authorization server and verifies the token signature locally. section 15.1

Test the protected endpoint

Obtain an access token through an OAuth 2 grant, then call the resource server with an Authorization: Bearer token header, such as curl 'http://localhost:9090/demo' --header 'Authorization: Bearer eyJraW…'. A valid JWT allows the authenticated request to reach the endpoint. section 15.1

JWTs avoid the authorization server call required by opaque-token introspection. Choose introspection instead when early revocation is needed or when the token would carry too much or overly sensitive data. section 13.2.2 section 15.5

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains the three-part structure, visibility, signature validation, Spring configuration, key retrieval, and endpoint testing.
Examples
Uses a three-part token sample, Spring configuration, a /demo endpoint, public-key metadata, and a Bearer-token curl request.
What it assumes
Assumes basic OAuth 2 actors and token flows while building the Spring resource-server configuration incrementally.
What you can do after
Build it from scratch by configuring Spring Security to validate JWTs against an authorization server’s public key set.
How it connects
Connects OAuth 2, OpenID Connect, authorization servers, resource servers, access tokens, claims, public-key cryptography, and introspection.

Also explained in

Compare how 11 books explain JSON Web Token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.