Skip to main content

Overview

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.

Authentication Flow

The authentication process follows these steps:
  1. Client sends credentials to /auth/login
  2. Controller receives and extracts the LoginRequest DTO
  3. Controller delegates token generation to JwtService
  4. Service creates a signed JWT with username and expiration
  5. Token is returned to the client as a plain string
  6. 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.

JWT Service Implementation

The JwtService class handles all token operations using the JJWT library.

Service Configuration

Marks this class as a Spring-managed service component, making it available for dependency injection throughout the application.

Token Generation

The generateToken() method creates a signed JWT containing the username and expiration time:

Token Claims

Value: Username provided during loginPurpose: Identifies the user this token belongs to
The subject is the primary identifier stored in the token. Subsequent requests can extract this to determine which user is making the request.
Value: Current timestamp in millisecondsPurpose: Records when the token was created
Useful for auditing, token refresh logic, and debugging authentication issues.
Value: Current time + 10 hours (36,000,000 milliseconds)Purpose: Defines token validity period
Calculation breakdown:
  • 1000 ms = 1 second
  • * 60 = 1 minute
  • * 60 = 1 hour
  • * 10 = 10 hours
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.
Algorithm: HMAC-SHA256 (HS256)Key: Pre-computed KEY from the secret
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.

Token Format

Generated tokens follow the standard JWT format:
  • Header: Algorithm and token type (Base64 encoded)
  • Payload: Claims including subject, issued at, expiration (Base64 encoded)
  • Signature: HMAC-SHA256 signature to verify integrity
You can decode tokens at jwt.io for debugging (never share tokens containing sensitive data).

Auth Controller

The AuthController provides the public API endpoint for authentication.

Login Endpoint

Endpoint: POST /auth/login Request Body:
Response: Plain text JWT string
Status Code: 200 OK
The controller currently logs authentication attempts to the console. Consider using a proper logging framework (SLF4J/Logback) for production applications.

Login Request DTO

The LoginRequest class structures incoming authentication requests:
Fields:
  • username - User’s unique identifier
  • password - User’s password (currently not validated)
Design Pattern: Plain Old Java Object (POJO) with getter/setter methods
Spring automatically deserializes JSON request bodies into this DTO using Jackson. The empty constructor is required for this process.

Security Best Practices

Current Implementation Gaps

The following security measures are missing from the current implementation:
  1. Password Verification: Credentials are not validated against the database
  2. Password Hashing: No BCrypt or similar hashing for stored passwords
  3. Token Validation: No middleware to verify tokens on protected endpoints
  4. Secret Management: Hardcoded secret key in source code
  5. HTTPS Enforcement: No SSL/TLS configuration specified
  6. Rate Limiting: No protection against brute force attacks
  7. Input Validation: No checks for empty/null username or password
Add a UserService to authenticate credentials:
Create a filter to verify JWTs on protected endpoints:
Add validation methods to JwtService:
Move the secret to application.properties:
Update JwtService:
Use Bean Validation annotations:
Update controller:
Hash passwords before storing in the database:
Update User entity:

Token Usage Example

Once implemented, clients should include tokens in the Authorization header:

Dependencies

The JWT implementation relies on these libraries from pom.xml:109-125:
  • jjwt-api: Core JWT API and interfaces
  • jjwt-impl: Implementation of JWT specification
  • 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.

Next Steps