Overview
InvestGo uses JWT (JSON Web Token) based authentication to secure API endpoints. The platform implements a stateless authentication mechanism where users receive a token after successful login, which must be included in subsequent requests.Authentication Flow
Token Storage
Store the received token securely on the client side (e.g., localStorage, sessionStorage, or memory).
Generating a Token
Endpoint
Request Body
Example: Login Request
Using the Token
Once you have a token, include it in theAuthorization header with the Bearer prefix for all protected endpoints.
Header Format
Example: Making Authenticated Requests
Getting Current User
Retrieve information about the currently authenticated user.Endpoint
Request
Response Example
Token Structure and Configuration
JWT Configuration
The JWT implementation uses the following configuration (fromJwtUtils.java:18,48):
- Secret Key:
examportal - Signature Algorithm: HS512 (HMAC-SHA512)
- Token Expiration: 10 hours (36,000,000 milliseconds)
- Claims: Subject (username) + issued/expiration timestamps
Token Claims
Each JWT token contains:| Claim | Description |
|---|---|
sub | Subject - the username of the authenticated user |
iat | Issued At - timestamp when the token was created |
exp | Expiration - timestamp when the token expires (10 hours from issue) |
Tokens are valid for 10 hours from the time of generation. After expiration, users must re-authenticate to receive a new token.
Token Validation Process
When a request is received with a token, the following validation occurs (JwtAuthenticationFilter.java:30-64):
Extract Token
The filter extracts the token from the
Authorization header, expecting format Bearer <token>.Error Handling
Common Authentication Errors
401 Unauthorized - Invalid Credentials
401 Unauthorized - Invalid Credentials
401 Unauthorized - Disabled User
401 Unauthorized - Disabled User
401 Unauthorized - User Not Found
401 Unauthorized - User Not Found
Token Expired
Token Expired
Cause: JWT token has passed its 10-hour expiration time.Console Log:
"El token ha expirado"Solution: Re-authenticate to obtain a new token.Invalid Token Format
Invalid Token Format
Cause: Token doesn’t start with “Bearer ” prefix or is malformed.Console Log:
"Token invalido, no empieza con bearer String"Solution: Ensure the Authorization header uses format: Bearer <token>Best Practices
Token Refresh Strategy
Since tokens expire after 10 hours, implement one of these strategies:- Re-authenticate before expiration: Check token expiration time and prompt user to re-login
- Silent refresh: Implement a refresh token mechanism (requires backend enhancement)
- Automatic re-login: Store credentials securely and auto-renew token (use with caution)
Handling Expired Tokens
Related Configuration Files
- JWT Utilities:
src/main/java/com/proyecto/integrador/configuraciones/JwtUtils.java - Authentication Filter:
src/main/java/com/proyecto/integrador/configuraciones/JwtAuthenticationFilter.java - Authentication Controller:
src/main/java/com/proyecto/integrador/controladores/AuthenticationController.java - Request/Response DTOs:
src/main/java/com/proyecto/integrador/dto/JwtRequest.javaandJwtResponse.java
Next Steps
- Learn about Role-Based Access Control
- Explore Protected Endpoints
- Review Security Configuration