EduTec Backend implements a stateless authentication system using JSON Web Tokens (JWT). This approach eliminates the need for server-side session storage, making the API scalable and suitable for distributed environments.
The application uses jjwt library version 0.11.5 for JWT operations, providing modern cryptographic standards and comprehensive token management.
Controller receives and extracts the LoginRequest DTO
Controller delegates token generation to JwtService
Service creates a signed JWT with username and expiration
Token is returned to the client as a plain string
Client includes token in subsequent requests via Authorization header
The current implementation does not validate credentials against the database. The login endpoint generates tokens for any username without password verification. This should be enhanced with proper authentication logic before production deployment.
// src/main/java/com/tecmilenio/edutec/security/JwtService.java:1-13package com.tecmilenio.edutec.security;import io.jsonwebtoken.Jwts;import io.jsonwebtoken.SignatureAlgorithm;import io.jsonwebtoken.security.Keys;import org.springframework.stereotype.Service;import java.util.Date;import java.security.Key;@Servicepublic class JwtService { // Clave de al menos 32 caracteres private static final String SECRET_KEY = "12345678910111213141516171819200"; // pre-generando la llave criptográfica solo cuando inicia la aplicación private static final Key KEY = Keys.hmacShaKeyFor(SECRET_KEY.getBytes());
@Service Annotation
SECRET_KEY
KEY
Marks this class as a Spring-managed service component, making it available for dependency injection throughout the application.
A 33-character secret key used for signing tokens. This key ensures token integrity and prevents tampering.
Security Risk: The secret key is hardcoded. In production environments, this should be externalized to environment variables or secure configuration management systems (e.g., AWS Secrets Manager, HashiCorp Vault).
A pre-computed cryptographic Key object generated using HMAC-SHA algorithm. Computing this once at class initialization improves performance by avoiding repeated key generation.
Tokens automatically become invalid after 10 hours, requiring users to re-authenticate. This balance between convenience and security can be adjusted based on your requirements.
Signature
Algorithm: HMAC-SHA256 (HS256)Key: Pre-computed KEY from the secret
.signWith(KEY, SignatureAlgorithm.HS256)
The signature ensures:
Integrity: Token data hasn’t been modified
Authenticity: Token was issued by this server
Non-repudiation: Only holders of the secret key can create valid tokens
HMAC-SHA256 is a symmetric algorithm, meaning the same key signs and verifies tokens. For asymmetric scenarios (microservices, distributed systems), consider RSA or ECDSA algorithms.
The controller currently logs authentication attempts to the console. Consider using a proper logging framework (SLF4J/Logback) for production applications.
public class LoginRequest { @NotBlank(message = "Username is required") @Size(min = 3, max = 50) private String username; @NotBlank(message = "Password is required") @Size(min = 8, message = "Password must be at least 8 characters") private String password;}
jjwt-jackson: JSON processing using Jackson library
Spring Security starter (spring-boot-starter-security) is commented out in the POM file, indicating a lightweight custom authentication approach without the full Spring Security framework.