Jwt Authentication Implementation
This project involves building a secure authentication system using JSON Web Tokens (JWT) for managing user sessions in a Spring Boot application. The system ensures that user requests are authenticated and authorized to access protected resources based on their role.
Functional Requirements: User Authentication:
Implement a login API to authenticate users via username and password. Upon successful authentication, generate a JWT token containing the user details. Token Generation & Claims:
JWT should be generated after successful login. Token should include claims such as: User ID Roles (admin, user) Expiration time The token must be signed using a secure algorithm such as HS256 or RSA. Token Validation:
Verify the JWT token on every API request to protected resources. Ensure the token has not expired and has not been tampered with. Reject requests with invalid, expired, or missing tokens. Access Control & Authorization:
Protect specific API routes and resources based on roles and permissions. Implement role-based access control (RBAC) where certain features or APIs are only available to specific user roles (e.g., admin). Logout Mechanism:
Provide an API for users to invalidate their tokens, simulating a logout. Optional: implement token blacklisting to ensure that the token is unusable after logout. Non-Functional Requirements: Security:
Ensure secure token storage on the client-side (e.g., in HTTP-only cookies or local storage). Use HTTPS for secure transmission of JWT tokens to avoid interception. Scalability:
The system should be able to handle multiple concurrent users, and the validation of tokens should not introduce significant performance overhead. Token Expiration and Refresh:
Implement token expiration to force re-authentication after a set period. Optional: include refresh tokens for long-lived sessions without requiring frequent re-logins. Error Handling:
Provide meaningful error messages and appropriate HTTP status codes for: Invalid credentials during login (401 Unauthorized) Token expiration or invalidation (403 Forbidden or 401 Unauthorized) Unauthorized access to protected resources (403 Forbidden) Technology Stack: Backend: Spring Boot Spring Security Java JWT Library Database: (Optional, if storing user details) MySQL/PostgreSQL