JWT Secure Auth System is a user authentication and authorization system built with Angular on the frontend and a Spring Boot-based backend. This project implements user signup, login, and JWT-based token authentication to secure API requests for fetching user details and other protected routes.
- User Signup: Open registration for new users.
- User Login: Secure login system with JWT token generation.
- JWT Authentication: Protects routes and user data using JSON Web Tokens.
- Token-Based Requests: Tokens are attached to requests to fetch user details securely.
- Secure Dashboard: After logging in, users can view their details on a personalized dashboard.
- Database Support: User details, including encrypted passwords, are securely stored in a MySQL database.
- Image Upload and Display: Users can upload images that are stored on the server and displayed in their dashboard.
- Framework: Angular
- Language: TypeScript, HTML, CSS
- Framework: Spring Boot (Java)
- Authentication: JWT (JSON Web Tokens)
- Database: MySQL
- Security: Spring Security for authentication and authorization
src/app: Contains Angular components, services, and routing logic for authentication (login/signup) and dashboard views.src/app/services: Contains services for handling user authentication, managing JWT tokens, and making HTTP requests.src/app/components: Includes reusable UI components such as the login form, signup form, and user dashboard.
src/main/java/com/jwt: Includes controllers, models, and services for handling user authentication and authorization.src/main/resources/application.properties: Backend configuration (e.g., database, security settings).- JWT Management: The backend generates and validates JWT tokens during user authentication.
- Spring Security: Ensures that only authenticated users with valid tokens can access protected routes.
- Signup: Users can create an account by signing up through the Angular frontend, filling their details. All registered users are stored in the MySQL database. After successful server side validation, user is redirected to login screen.
- Login: After login, the backend generates a JWT token, which is returned to the client along with response.
- Token Storage: The JWT token is securely stored in the client's browser (localStorage).
- Authenticated Requests: For subsequent requests to fetch user details or access protected resources, the frontend attaches the JWT token in the Authorization header (
Bearer <token>). - Token Validation: The backend verifies the token's validity for each protected request using Spring Security.
Below is the flow diagram that outlines the authentication process.
Figure 1: JWT Authentication Flow Diagram
Figure 2: Landing Page of Application
Figure 3: User Signup Interface
Figure 4: User Login Interface
Figure 5: User Dashboard After Successful Login
Figure 6: User Details fetched by User
- Node.js: Install the latest version of Node.js here.
- Angular CLI: Install Angular CLI globally using the command:
npm install -g @angular/cli
- Java: Ensure that JDK 8 or higher is installed.
- Maven: Install Maven for building the Spring Boot backend.
- MySQL: Set up a MySQL database for storing user data.
- Clone the repository:
git clone https://github.com/your-username/jwt-secure-auth-system.git
cd jwt-secure-auth-system/backend- Configure the database in the application.properties file:
spring.application.name=jwtAuthentication
server.port=8081
spring.datasource.name= ds
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB- Build and run the backend:
mvn clean install
mvn spring-boot:run- Navigate to the frontend folder and install the required dependencies:
cd jwt-secure-auth-system/frontend
npm install- Run the Angular development server:
ng serve- Open your browser and navigate to
http://localhost:4200.
- POST
/signup: Register a new user (stores user details in MySQL). - POST
/token: Log in an existing user and receive a JWT token.
- GET
/getuser: Fetch user details (requires JWT token). - Other protected routes: All other routes require a valid JWT token for access and are secured using Spring Security.
- Token Expiry: Ensure JWT tokens have an expiration time to prevent misuse.
- HTTPS: Always use HTTPS for secure communication.
- Token Storage: Store the JWT token securely in localStorage or sessionStorage in the browser. Avoid using cookies unless they're secured and HTTP-only.
- Spring Security: Leverages Spring Security to validate and authenticate tokens on protected routes.
-
User Login Request
The user sends a login request (POST /token) with their username and password in the body. -
Validate Input
The backend validates that both the username and password fields are not null or empty. If either is missing, aBAD_REQUESTresponse is returned. -
Authentication Process
TheAuthenticationManageris used to authenticate the user's credentials by comparing them with stored user data (usingUsernamePasswordAuthenticationToken). If authentication fails, an exception is thrown. -
Load User Details
If authentication is successful, the user’s details (username, roles, etc.) are fetched from theCustomUserDetailsService. -
Generate JWT Token
TheJwtUtilclass generates a JWT token based on the authenticated user's details (username, roles, etc.). -
Return JWT Token
The generated token is wrapped in aJwtResponseobject and returned to the client with aCREATEDresponse status. -
Client Stores Token
The client (frontend) receives the token, stores it (inlocalStorage), and includes it in the Authorization header (Bearer <token>) for subsequent requests to protected routes.
- Password Reset: Add functionality for users to reset their passwords.
- Role-Based Access Control: Implement roles (e.g., admin, user) to restrict access to certain endpoints.
- Refresh Tokens: Add refresh tokens to extend the user session without requiring re-login.
Contributions are welcome! Please fork this repository, create a new branch for your feature or bug fix, and submit a pull request.