Overview
The User Management System uses JWT (JSON Web Tokens) for stateless authentication. Security is configured through Spring Security with a custom JWT filter chain.JWT Configuration
JWT settings are configured in the profile-specific properties files.Development Settings
application-dev.properties
Production Settings
application-prod.properties
Secret Key Setup
Generating a Secure Secret
Your JWT secret should be at least 256 bits (32 characters) long. Generate a secure secret:Setting the Secret
- Environment Variable
- Properties File
- Docker Compose
Using environment variables is the recommended approach for production deployments to avoid committing secrets to version control.
Token Expiration Settings
Thejwt.expiration property defines token lifetime in milliseconds.
Common Expiration Times
| Duration | Milliseconds | Configuration |
|---|---|---|
| 15 minutes | 900000 | jwt.expiration=900000 |
| 1 hour | 3600000 | jwt.expiration=3600000 |
| 24 hours | 86400000 | jwt.expiration=86400000 |
| 7 days | 604800000 | jwt.expiration=604800000 |
| 30 days | 2592000000 | jwt.expiration=2592000000 |
Recommendations
- Short-lived tokens (15-60 min): High security applications
- Medium-lived tokens (1-24 hours): Standard applications
- Long-lived tokens (7-30 days): Mobile applications with refresh token strategy
JWT Utility Class
TheJwtUtil class handles token generation and validation:
JwtUtil.java
- Uses HMAC256 algorithm for signing
- Stores username in the
subjectclaim - Stores user role in a custom
roleclaim - Validates token signature and expiration
Security Filter Chain
The security configuration is defined inSecurityConfig.java:
SecurityConfig.java
Security Features
CSRF Protection
CSRF Protection
CSRF is disabled because JWT authentication is stateless and doesn’t use cookies.
Stateless Sessions
Stateless Sessions
Sessions are disabled for true stateless authentication.
Password Encoding
Password Encoding
BCrypt is used for secure password hashing with a default strength of 10.
Authorization Rules
Access control is configured per endpoint:| Endpoint Pattern | Required Authority | Description |
|---|---|---|
/auth/** | None (permitAll) | Authentication endpoints |
/users/me | ROLE_USER | User profile access |
/admin/users | ROLE_ADMIN | Admin user management |
Adding Custom Rules
Add new authorization rules in theSecurityFilterChain:
JWT Authentication Filter
TheJwtAuthenticationFilter extracts and validates JWT tokens from requests:
JwtAuthenticationFilter.java
Filter Flow
- Extract token from
Authorizationheader - Validate token using
JwtUtil - Extract claims (username and role)
- Create authentication object
- Set security context for the request
- Continue filter chain
Using JWT Tokens
Authentication Request
Response
Authenticated Request
Security Best Practices
Strong Secrets
Use randomly generated secrets of at least 256 bits. Never use default or predictable secrets in production.
Short Expiration
Set appropriate token expiration times. Implement refresh tokens for longer sessions.
HTTPS Only
Always use HTTPS in production to prevent token interception.
Secret Rotation
Implement a strategy for rotating JWT secrets periodically.
Environment-Specific Security
- Development
- Production
Troubleshooting
Token Validation Fails
Check:- Secret matches between token generation and validation
- Token hasn’t expired
- Token format is correct (Bearer prefix)
- Token hasn’t been tampered with
Unauthorized Access
Verify:- Token is included in
Authorizationheader - Token has required role/authority
- Endpoint requires authentication
- Filter chain is properly configured
Invalid Signature
Causes:- JWT secret changed after token generation
- Token modified after signing
- Wrong algorithm used for verification