What is a JWT Token?
JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. In this API, JWTs are used to securely transmit authentication information between the client and server.Token Structure
A JWT consists of three parts separated by dots (.):Example Token
JWT Claims
The Library Management API includes the following claims in each JWT:| Claim | Type | Description | Example |
|---|---|---|---|
iss | String | Issuer - identifies who issued the token | Configured via security.jwt.user.generator |
sub | String | Subject - the username of the authenticated user | "johndoe" |
authorities | String | Comma-separated list of user authorities/roles | "" (empty in current implementation) |
iat | Number | Issued At - timestamp when token was created | 1709640000 |
exp | Number | Expiration - timestamp when token expires | 1709641800 |
jti | String | JWT ID - unique identifier for the token | "12345678-1234-1234-1234-1234567890ab" |
nbf | Number | Not Before - timestamp before which token is invalid | 1709640000 |
The
exp (expiration) claim is set to 30 minutes (1800000 milliseconds) from the token creation time.Token Generation
Tokens are generated during the login process using theJwtUtils.createToken() method:
Key Generation Details
Token Validation
Every request to a protected endpoint must include a valid JWT token. The validation process is handled by theJwtTokenValidator filter:
Validation Checks
The token validation process verifies:- Signature Verification: Ensures the token hasn’t been tampered with
- Issuer Verification: Confirms the token was issued by this API
- Expiration Check: Validates the token hasn’t expired
- Format Validation: Ensures the token structure is valid
Using JWT Tokens in Requests
To access protected endpoints, include the JWT token in theAuthorization header with the Bearer prefix:
Token Extraction Methods
TheJwtUtils class provides several methods to extract information from decoded tokens:
Token Expiration and Refresh
Expiration Time
- Duration: 30 minutes (1800000 milliseconds)
- Set at: Token creation time
- Calculated as:
current_time + 1800000
Handling Expiration
Client Detects Expiration
Client receives 401 Unauthorized response or proactively checks token expiration
Configuration Properties
The JWT system uses the following application properties:| Property | Description | Type |
|---|---|---|
security.jwt.user.generator | Issuer identifier for JWT tokens | String |
security.jwt.key.private | Private key for HMAC256 signing | String |
These properties should be configured in
application.properties or application.yml and kept secure, especially in production environments.Security Best Practices
For API Developers
- Keep the private key (
security.jwt.key.private) secret and secure - Use environment variables for sensitive configuration
- Never commit the private key to version control
- Rotate keys periodically for enhanced security
For API Consumers
- Store tokens securely (avoid localStorage for sensitive applications)
- Never expose tokens in URLs or logs
- Implement token refresh logic before expiration
- Clear tokens on logout
- Use HTTPS for all API communication
Common Token Errors
| Error | Cause | Solution |
|---|---|---|
| Token signature verification failed | Token was tampered with or signed with different key | Obtain a new token through login |
| Token expired | Token age exceeds 30 minutes | Re-authenticate to get a new token |
| Invalid issuer | Token was not issued by this API | Use tokens only from this API |
| Malformed token | Token format is incorrect | Ensure token is properly formatted |
| Missing Authorization header | Request doesn’t include the token | Add Authorization: Bearer <token> header |
Related Resources
Login Process
Learn how to obtain JWT tokens through login
Registration
Create a new account to start using the API
Authentication Overview
Understand the complete authentication system
API Reference
View detailed authentication endpoint documentation