This project is developed as part of the Banque Misr Backend Internship program, focusing on mastering Spring Boot, RESTful APIs, security, and enterprise-grade application design.
A secure, role-based online voting system built with Spring Boot, designed to manage digital elections with JWT authentication, vote integrity, and real-time results.
This backend API supports:
- Admin and voter roles
- Secure login with JWT
- Candidate registration
- Voter assignment
- One-time voting within a time window
- Real-time result calculation
- RESTful design with validation and error handling
| Category | Technology |
|---|---|
| Framework | Spring Boot (Java 17+) |
| Build Tool | Maven |
| Database | H2 (Dev), MySQL (Production) |
| Security | Spring Security + JWT (JWT) |
| Validation | Jakarta Bean Validation (Hibernate Validator) |
| ORM | Spring Data JPA / Hibernate |
| API Testing | Postman ,Swagger UI |
src/main/java/com/onlinevotingsystem/
│
├── model/ → JPA entities (User, Candidate, Election, Vote, Role enum)
├── dto/ → Data Transfer Objects
│ ├── request/ → Input DTOs (UserRegisterDto, VoteDto)
│ └── response/ → Output DTOs (UserResponseDto, ResultDto)
├── repository/ → Spring Data JPA repositories
├── service/ → Business logic
├── controller/ → REST endpoints (AdminController, VoterController, AuthController)
├── security/ → JWT filters, config, utilities
├── exception/ → Custom exceptions and global handler
└── OnlineVotingSystemApplication.java → Main class
- Java 17 or higher
- Maven
- IDE (IntelliJ IDEA, Eclipse, or VS Code)
- MySQL
git clone https://github.com/Eng-AmanyMohamed/online-voting-system.git
cd online-voting-systemEdit src/main/resources/application.properties (for MySQL):
spring.datasource.url=jdbc:mysql://localhost:3306/bank_system
spring.datasource.username=yourusername
spring.datasource.password=yourpassword
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Using Maven (Command Line)
mvn spring-boot:run
All secured endpoints require a valid JWT in the header:
Authorization: Bearer <your-jwt-token>
- POST
/api/users/registerRegister a new user (ِAdmin,Voter). - POST
/api/auth/loginAuthenticate a user (admin or voter) and receive a JWT.
- POST
/api/admin/electionsCreate a new election with a voting time window. (Admin only) - POST
/api/admin/voters/assignAssign a registered voter to an election. (Admin only)
- POST
/api/admin/candidatesRegister a new candidate for an election. (Admin only) - GET
/api/voter/candidatesGet the list of all candidates in the current election. (Voter only)
- POST
/api/voter/voteCast a vote for a candidate. Only allowed once per voter and within election time. (Voter only)
- GET
/api/admin/resultsGet real-time election results sorted by vote count (descending). (Admin only)
- JWT Required: All endpoints (except /login) require a valid token in Authorization: Bearer header.
- Role-Based Access:
- Admin: Full access
- Voter: Vote & view candidates only
- Input Validation:
- Email format, password strength, required fields
- Invalid data → 400 Bad Request
- Vote Protection:
- One vote per user
- Voting only during startTime to endTime
- Attempt outside window → 403 Forbidden with "Voting is currently closed."
- Returns structured JSON errors:
json
{
"timestamp": "2025-04-05T10:00:00",
"status": 400,
"error": "Bad Request",
"message": "Email is required",
}
- Handles:
- Invalid login.
- Duplicate vote.
- Resource not found.
- Voting closed.
- Unauthorized access.
This project was a collaborative effort with an outstanding team:
- Amany Mohamed
- Mohamed Mostafa
- Youssef Ahmed
- Touka Mohamed
- Leena Hesham
- Esraa Tarek
- Ayten Aaser
- Mariam Ayman