Overview
InvestGo implements a robust security architecture using Spring Security with JWT-based authentication. The system provides role-based access control (RBAC), password encryption, and stateless session management.Security Architecture
The security configuration is implemented inMySecurityConfig.java:23 and includes:
- JWT-based stateless authentication
- BCrypt password encryption
- Role-based authorization (ADMIN & INVERSIONISTA)
- CORS and CSRF configuration
- Custom authentication entry point
- Security filter chain
Spring Security Configuration
Core Components
Security Features
| Feature | Configuration | Description |
|---|---|---|
| Authentication | JWT-based | Stateless token authentication |
| Password Hashing | BCrypt | Industry-standard password encryption |
| Session Management | STATELESS | No server-side sessions |
| CSRF Protection | Disabled | Not needed for stateless JWT |
| CORS | Disabled (Controller-level) | Handled via @CrossOrigin annotations |
| Method Security | Enabled | @PreAuthorize annotations supported |
Role-Based Access Control
User Roles
InvestGo defines two primary roles:ADMIN (Role ID: 2)
ADMIN (Role ID: 2)
Capabilities:
- Full system access
- User management
- Factoring configuration
- Risk level management
- Bank and currency management
- View all transactions and investments
- Username:
jamie - Password:
Admin12345(BCrypt encrypted) - Email:
[email protected] - Initial wallet balance: S/. 10,000,000
INVERSIONISTA (Role ID: 1)
INVERSIONISTA (Role ID: 1)
Capabilities:
- View available invoices
- Make investments
- Manage personal portfolio
- Wallet operations (deposit/withdrawal)
- View own transactions
- Profile management
Implementing Role-Based Access
Use@PreAuthorize annotations to restrict endpoints by role:
Protected vs Public Endpoints
Public Endpoints (No Authentication Required)
FromMySecurityConfig.java:59:
| Endpoint Pattern | Description |
|---|---|
POST /generate-token | User authentication and token generation |
/api/** | All endpoints under /api/ path |
OPTIONS * | All OPTIONS requests (for CORS preflight) |
The
/api/** pattern appears to be intended for public API endpoints or user registration. Verify actual implementation for specific use cases.Protected Endpoints (Authentication Required)
All other endpoints require valid JWT token:GET /actual-usuario- Get current user information/facturas/**- Invoice management/inversiones/**- Investment operations/usuarios/**- User management/transacciones/**- Transaction history/cartera/**- Wallet operations
Password Encryption
BCrypt Configuration
InvestGo uses BCrypt for password hashing (MySecurityConfig.java:41-43):
Password Hashing in Practice
From the initial admin user creation (SistemaFactoringBackendApplication.java:74):
- Adaptive hashing function
- Automatic salt generation
- Configurable work factor (default: 10)
- One-way encryption (cannot be decrypted)
Example: User Registration with Password Encryption
Security Filter Chain
Filter Execution Order
JwtAuthenticationFilter
Executes before
UsernamePasswordAuthenticationFilter to extract and validate JWT tokens.Custom Authentication Entry Point
When authentication fails,JwtAuthenticationEntryPoint.java:19 handles the response:
- Returns HTTP 401 (Unauthorized)
- Response body:
"USUARIO NO AUTORIZADO" - Triggered when no valid authentication is present
CORS Configuration
Controller-Level CORS
While CORS is disabled in security config (MySecurityConfig.java:56-57), it’s enabled at the controller level:
Recommended CORS Configuration for Production
Session Management
Stateless Sessions
FromMySecurityConfig.java:65:
- No HTTP sessions created or used
- No cookies for session tracking
- Each request must include JWT token
- Horizontal scaling is easier (no session replication needed)
- No server-side state for authentication
Stateless architecture makes InvestGo highly scalable. Each request is independently authenticated without server-side session storage.
Security Testing
Testing Authentication
Testing Role-Based Access
Common Security Issues
Token Not Recognized
Token Not Recognized
Symptoms: Always receiving 401 Unauthorized even with valid tokenPossible Causes:
- Missing “Bearer ” prefix in Authorization header
- Token copied with extra whitespace or line breaks
- Token expired (check
expclaim) - Secret key mismatch between token generation and validation
Authorization: Bearer <token>CORS Errors in Browser
CORS Errors in Browser
Symptoms: Browser console shows CORS policy errorsPossible Causes:
- Controller missing
@CrossOriginannotation - OPTIONS requests not properly configured
- Credentials mode mismatch
@CrossOrigin to controllers or configure global CORSPassword Authentication Fails
Password Authentication Fails
Symptoms: Login fails with “Credenciales ivalidas” for correct passwordPossible Causes:
- Password not BCrypt encoded in database
- BCrypt encoder not configured correctly
- Password encoding done multiple times
Access Denied for Authorized User
Access Denied for Authorized User
Security Best Practices
Related Configuration Files
- Main Security Config:
src/main/java/com/proyecto/integrador/configuraciones/MySecurityConfig.java - JWT Filter:
src/main/java/com/proyecto/integrador/configuraciones/JwtAuthenticationFilter.java - Entry Point:
src/main/java/com/proyecto/integrador/configuraciones/JwtAuthenticationEntryPoint.java - User Details Service:
src/main/java/com/proyecto/integrador/servicios/impl/UserDetailsServiceImpl.java
Next Steps
- Configure JWT Authentication
- Set up Database Connection
- Explore API Endpoints