An open API service indexing awesome lists of open source software.

https://github.com/tushar-swarnkar/expense-tracker

Expense Tracker is a secure and role-based backend application built using Spring Boot and JWT that allows users to log, manage, and retrieve their personal expenses month-wise and year-wise. Admins can manage user roles and ensure data integrity, making it a robust system for financial tracking and control.
https://github.com/tushar-swarnkar/expense-tracker

hibernate java jjwt-api jjwt-impl jjwt-jackson jwt-authentication jwt-authorization lombok maven maven-pom mysql mysql-workbench spring-data-jpa spring-mvc spring-security springboot

Last synced: 2 months ago
JSON representation

Expense Tracker is a secure and role-based backend application built using Spring Boot and JWT that allows users to log, manage, and retrieve their personal expenses month-wise and year-wise. Admins can manage user roles and ensure data integrity, making it a robust system for financial tracking and control.

Awesome Lists containing this project

README

          

# Expense Tracker (Backend)

## ๐Ÿ“Œ Project Overview
This repository contains the backend implementation of the **Expense Tracker** application built using **Java** and **Spring Boot**. It provides secure APIs for user authentication, expense logging, and data retrieval, with role-based access control using **JWT** and **Spring Security**.

## ๐Ÿ“Œ Features
โœ”๏ธ User Registration & Login
โœ”๏ธ JWT-based Authentication
โœ”๏ธ Role-Based Access Control (User/Admin)
โœ”๏ธ Secure Endpoints using Spring Security
โœ”๏ธ Users can log their monthly expenses
โœ”๏ธ Fetch all expenses for the logged-in user
โœ”๏ธ Filter expenses by month or year
โœ”๏ธ Admin can update user roles (e.g., promote to admin)
โœ”๏ธ Clean error handling and logging

## ๐Ÿ› ๏ธ Tech Stack
- **Java 17**
- **Spring Boot 3.x**
- **Spring Security 6**
- **JWT (Java JWT - jjwt)**
- **Maven**
- **MySQL**
- **Postman** (for testing)

### ๐Ÿ“ฆ Spring Boot Dependencies
- **spring-boot-starter-web** โ€“ REST APIs
- **spring-boot-starter-security** โ€“ Authentication & Authorization
- **spring-boot-starter-data-jpa** โ€“ Database interaction
- **jjwt-api, jjwt-impl, jjwt-jackson** โ€“ JWT token generation and validation
- **spring-boot-devtools** โ€“ Development-time utilities
- **lombok** โ€“ Reduces boilerplate code

## ๐Ÿ” Authentication & Security

### ๐Ÿ”‘ JWT Package Overview
- **JwtUtil.java** โ€“ Utility for creating and validating JWTs
- **JwtFilter.java** โ€“ Filters requests and sets authentication context from JWT
- **CustomUserDetailsService.java** โ€“ Loads user credentials from the database for Spring Security
- **SecurityConfig.java** โ€“ Spring Security configuration with JWT filter and role-based access

## ๐Ÿ“– API ENDPOINT GUIDE

### ๐Ÿ‘ฅ Public Endpoints
- **`POST /user/signup`** โ€“ Register a new user
- **`POST /user/login`** โ€“ Authenticate user and receive JWT token

### ๐Ÿ” Secured Endpoints (require JWT token)

#### ๐Ÿงพ Expenses
- **`POST /expense/add`** โ€“ Add a new expense
- **`GET /expense/all`** โ€“ Get all expenses for the logged-in user
- **`GET /expense/month/{month}`** โ€“ Get expenses by month
- **`GET /expense/year/{year}`** โ€“ Get expenses by year

#### โš™๏ธ Admin-only
- **`PUT /admin/set-role/{userId}`** โ€“ Promote user to admin

## Spring configurations for the project:
### add the following configurations in the "/src/main/resources/application.properties" file:
```properties
# Application Info
spring.application.name=expense-tracker

# Server Configuration
server.port=9999

# Database Configuration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # tells Spring Boot to use the MySQL Connector/J (JDBC driver)
spring.datasource.url=jdbc:mysql://localhost:3306/expense_tracker # configuration key to set the JDBC URL
spring.datasource.username=YOUR_DB_USERNAME
spring.datasource.password=YOUR_DB_PASSWORD

# JPA & Hibernate Settings
spring.jpa.show-sql=true # shows the SQL query
spring.jpa.hibernate.ddl-auto=update # automatically updates your database schema
spring.jpa.properties.hibernate.format_sql=true # formatting the SQL query in readable form
```

## ๐Ÿ“‚ Package Structure
```yaml
๐Ÿ“ฆ com.stew.expense_tracker
โ”œโ”€โ”€ ๐Ÿ“‚ config # Spring Security Configuration
โ”œโ”€โ”€ ๐Ÿ“‚ constants # Enum for user roles
โ”œโ”€โ”€ ๐Ÿ“‚ controller # REST controllers
โ”œโ”€โ”€ ๐Ÿ“‚ model # Entity classes (User, Expense)
โ”œโ”€โ”€ ๐Ÿ“‚ repository # JPA Repositories
โ”œโ”€โ”€ ๐Ÿ“‚ service # Service classes with business logic
โ”œโ”€โ”€ ๐Ÿ“‚ jwt # JWT utilities and filter
โ”œโ”€โ”€ ๐Ÿ“‚ wrapper # DTOs for requests/responses
โ””โ”€โ”€ ๐Ÿ“„ Application.java # Main Spring Boot class