JwtTokenProvider is not a Spring or JWT-library class. It is a naming convention for a small component in your own code that creates, signs, parses and validates JSON Web Tokens, so the rest of the application never touches the token library directly. In a stateless Spring Security setup it is the one place that knows your signing key, token lifetime and claim names.
A typical flow is: the login endpoint authenticates credentials and asks the provider for a token, and a filter on every later request asks the provider to validate the token and read the user and roles from it.
| Responsibility | What it does |
|---|---|
| Generate | Builds a token with subject, roles, issued-at and expiry, then signs it |
| Parse | Verifies the signature and returns the claims |
| Validate | Rejects expired, malformed or tampered tokens |
| Extract | Returns the username and authorities for the security context |
Keeping this logic in one class makes it easy to test and to replace, for example when you move from a shared secret to asymmetric keys.
The example uses the io.jsonwebtoken:jjwt-api library (with jjwt-impl and jjwt-jackson at runtime), version 0.12 or later. Older tutorials use Jwts.parser().setSigningKey(...) and SignatureAlgorithm.HS256, which are deprecated in 0.12 and removed in later releases.
Keys.hmacShaKeyFor picks HS256, HS384 or HS512 from the key length, and it rejects keys shorter than 256 bits, so the secret must be at least 32 bytes. Load it from an environment variable or secret manager through app.jwt.secret, never from source code. The original pattern of a field such as "mySecretKey" is both too short to work with current library versions and unsafe.
Parsing already throws on a bad signature or an expired token (ExpiredJwtException is a JwtException), so isValid is a thin wrapper that you can use in a filter.
A filter extracts the Bearer token from the Authorization header and uses the provider to build the authentication:
Register it in a SecurityFilterChain bean, with sessions disabled, before UsernamePasswordAuthenticationFilter. WebSecurityConfigurerAdapter was removed in Spring Security 6, so the old configure(HttpSecurity) style no longer compiles.
If your application only validates tokens issued by an identity provider such as Keycloak or another OAuth 2.0 server, you do not need to write one. Spring Security's resource server support does it for you: add spring-boot-starter-oauth2-resource-server, set spring.security.oauth2.resourceserver.jwt.issuer-uri, and call http.oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults())). It is better tested than hand-written code and handles key rotation through the JWKS endpoint.
No. It is a convention for a helper class in your project. Spring Security supplies the filter chain and, for resource servers, a JwtDecoder.
In an environment variable, secret manager or vault, injected through configuration. Rotate it on a schedule and never commit it to Git.
A stateless token cannot be recalled by itself. Keep lifetimes short, drop the refresh token on logout, and use a deny list keyed by token id (jti) if immediate revocation is required.
HS256 is fine for a single service that signs and verifies. Choose RS256 or ES256 when verifiers should not be able to create tokens.
JwtTokenProvider centralises JWT creation, parsing and validation so the security filter stays small. Use the current jjwt builder and parser API, load a long random key from configuration, keep tokens short-lived, and prefer Spring Security's built-in resource server when an external identity provider issues the tokens.