Overview
The User Management System uses JSON Web Tokens (JWT) for stateless authentication. When users log in successfully, they receive a JWT token that must be included in subsequent requests to access protected endpoints.How JWT Authentication Works
The authentication flow follows these steps:Credential Verification
The system validates the credentials against the stored user data using BCrypt password hashing.
Token Generation
If credentials are valid, a JWT token is generated containing the username and role.
Token Generation Process
Tokens are generated by theJwtUtil class when a user successfully logs in. The token includes:
- Subject: The username
- Role Claim: The user’s role (e.g.,
ROLE_USERorROLE_ADMIN) - Expiration: Token expiry time based on the configured duration
Code Reference
The token generation happens inJwtUtil.java:20-26:
UserServiceImpl.java:59:
The JWT secret key and expiration time are configured in
application.properties using the properties jwt.secret and jwt.expiration.Token Validation and Extraction
When a request arrives with a JWT token, theJwtAuthenticationFilter validates and extracts information from it.
Filter Process
TheJwtAuthenticationFilter extends Spring’s OncePerRequestFilter to ensure it runs exactly once per request. Here’s what happens in JwtAuthenticationFilter.java:23-44:
- Extract Token: The filter checks for an
Authorizationheader with aBearerprefix - Validate Token: Uses
JwtUtilto validate the token signature and expiration - Extract Claims: Retrieves username and role from the token
- Set Authentication: Creates a Spring Security authentication object and stores it in the SecurityContext
Token Validation Logic
ThevalidateToken method in JwtUtil.java:28-36 verifies the token signature using HMAC256:
How to Include Tokens in Requests
Once you receive a token from the login endpoint, include it in theAuthorization header of your HTTP requests.
Example Login Request
Response
Using the Token
Include the token in subsequent requests:The token must be prefixed with
Bearer (note the space after “Bearer”). The filter specifically looks for this format when extracting the token from the header.Security Configuration
The authentication filter is integrated into Spring Security’s filter chain inSecurityConfig.java:32:
Session Management
The system is configured for stateless authentication:- No server-side sessions are created
- Each request is authenticated independently via the JWT token
- The server doesn’t store any session state
Password Security
User passwords are hashed using BCrypt before storage. TheUserServiceImpl uses Spring Security’s PasswordEncoder for secure password handling:
BCrypt is a one-way hashing function with built-in salt, making it highly resistant to rainbow table attacks and brute-force attempts.
Public Endpoints
The following endpoints are publicly accessible without authentication:POST /auth/signup- User registrationPOST /auth/login- User login
Next Steps
Authorization
Learn about role-based access control
Roles
Understand user roles and permissions