構建 CivicGemma:以 Gemma 4 打造具防篡改功能的電子投票微服務平台
Building CivicGemma: A Tamper-Evident E-Voting Microservices Platform with Gemma 4 🗳️
CivicGemma 是一個開源電子投票試點平台,面向地方選舉,採用 Java 21、Spring Boot 3.4.2、Angular 22 微服務架構及 Gemma 4。平台以應用程式檢查、資料庫唯一索引和 SHA-256 數碼封印防止重複投票,並提供多模態選民身份證核驗及多語言政綱解説。
🚀 TL;DR
During Hacktoberfest Hack Day Bhubaneswar × DatrixLabs, I built CivicGemma — an open-source, full-stack microservices e-voting pilot system designed specifically for local body elections (Panchayats, Municipalities, and Wards).
It combines Java 21 & Spring Boot 3.4 microservices, Angular 22, and Gemma 4 multimodal AI to deliver double-voting prevention, cryptographic SHA-256 ballot sealing, and multilingual voter assistance.
💡 The Problem: Why Local Body Elections?
Grassroots democracy is where policy directly impacts daily life — from local roads to sanitation and water supply. However, local body elections face unique logistical hurdles:
- Paper Ballot Inefficiencies & Fraud: High printing costs, slow manual counting, and vulnerability to tampering.
- Double-Voting Risks: Malicious voters registered across adjacent wards attempting multiple ballots.
- Information Asymmetry: Citizens often don't know who their ward candidates are or what their local manifestos promise.
We asked: Can we build a transparent, auditable, and accessible digital voting platform that guarantees ballot integrity while empowering voters with AI?
That was the birth of CivicGemma.
🏗️ System Architecture: The Microservices Ecosystem
Rather than building a monolithic prototype, we architected CivicGemma around the Database-per-Service pattern using Spring Cloud 2024 and Spring Boot 3.4.2:
┌─────────────────────────┐
│ Angular 22 Frontend │
└────────────┬────────────┘
│ HTTP / REST
▼
┌─────────────────────────┐
│ Spring Cloud Gateway │ (Port 8080)
└────────────┬────────────┘
│ lb://
┌──────────────┬─────────────┼─────────────┬──────────────┐
▼ ▼ ▼ ▼ ▼
┌──────────────┐┌────────────┐┌────────────┐┌────────────┐┌────────────┐ │ Auth Service ││Voter Service││Voting Service│Result Service│Audit Service│ │ (Port 8086) ││ (Port 8081) ││ (Port 8084) ││ (Port 8085) ││ (Port 8087) │ └──────┬───────┘└─────┬──────┘└─────┬──────┘└─────┬──────┘└─────┬──────┘ │ │ │ │ │ [Auth DB] [Voter DB] [Voting DB] [Result DB] [Audit DB]
Key Components:
- Eureka Discovery Server (8761): Dynamic registry enabling microservices to locate each other without hardcoded IPs.
- Spring Cloud API Gateway (8080): Reverse proxy handling routing, CORS policies, and centralized security.
-
7 Domain Services:
Auth,Voter,Election,Candidate,Voting,Result, andAudit. - Database-per-Service: Each microservice has its own isolated MySQL database schema to ensure zero tight coupling.
🛡️ Core Innovation #1: Unbreakable Double-Voting Defense
In electronic voting, preventing race conditions (e.g., clicking "Vote" simultaneously across multiple tabs) is paramount. We implemented a three-tier defense:
- Application Layer Check:
if (voteRepository.existsByElectionIdAndVoterId(dto.getElectionId(), dto.getVoterId())) {
throw new IllegalStateException("Voter has already cast a ballot.");
}
Database Engine Constraint (The Hard Wall): We enforce a compound unique index constraint at the database level:
ALTER TABLE votes ADD CONSTRAINT uk_election_voter UNIQUE (election_id, voter_id);
Cryptographic SHA-256 Ballot Sealing: Every ballot generates a tamper-evident digital seal returned to the voter as a verifiable receipt:
String rawData = voterId + ":" + electionId + ":" + candidateId + ":" + timestamp + ":" + UUID.randomUUID();
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(rawData.getBytes(StandardCharsets.UTF_8));
// Returns 64-character hexadecimal receipt hash
🤖 Core Innovation #2: Gemma 4 Multimodal Civic AI
We leveraged Google's open-weights Gemma 4 model to solve two real-world accessibility challenges:
1. Multimodal Voter ID (EPIC) Verification
Elderly or rural voters can upload an image of their Electors Photo Identity Card (EPIC). Gemma 4's vision capabilities inspect the card, verify document legibility, extract the identifier, and cross-validate with the voter-service registry.
2. Multilingual Manifesto Explainer
Local voters can ask natural language questions in English, Hindi, or Odia:
"Who is running in Ward 12?"
"Summarize Candidate Arjun Sharma's key promises in 3 bullet points."
"Which candidate has promised better water drainage?"
Gemma parses structured candidate manifestos and delivers impartial, concise summaries.
🛠️ The Tech Stack
Domain Technologies
Backend Java 21 LTS, Spring Boot 3.4.2, Spring Cloud 2024 (Eureka, Gateway, OpenFeign)
Frontend Angular 22 (Standalone Components, SSR, Tailwind CSS)
Databases MySQL (Isolated schema per microservice)
AI / ML Google Gemma 4 / Gemini API
Security Stateless JWT (HMAC-SHA256), BCrypt, SHA-256
DevOps Maven Multi-Module, Automated Batch Launchers (start-all.bat)
🧗 Challenges Faced & Lessons Learned
1. The JDK Compiler vs. Lombok Dilemma
During compilation, we hit a compiler error:
Fatal error compiling: java.lang.ExceptionInInitializerError: com.sun.tools.javac.code.TypeTag :: UNKNOWN
We discovered that the host environment was compiling with bleeding-edge JDK 25/26, which introduced breaking internal changes to com.sun.tools.javac.code.TypeTag that broke annotation processors like Lombok. We diagnosed and resolved this by pinning our toolchain to Eclipse Temurin JDK 21 LTS, restoring clean, reproducible builds.
2. Distributed State vs. Network Partitions
When voting-service records a ballot, it must sync hasVoted = true with voter-service. While OpenFeign handles synchronous calls gracefully, a network partition requires resilience. We architected fallback exception handlers and designed the schema so that the ballot ledger remains authoritative even if the voter metadata service experiences latency.
🔮 What's Next for CivicGemma?
Zero-Knowledge Proofs (ZKP): Implementing zk-SNARKs to achieve complete ballot unlinkability while mathematically guaranteeing eligibility.
Consortium Blockchain (Hyperledger Fabric): Moving the audit log service onto a distributed ledger so no single administrative authority can modify election logs.
Offline Polling Station Sync: Edge-caching for rural booths with intermittent connectivity.
🙏 Acknowledgements
Huge thanks to Major League Hacking (MLH), DatrixLabs, and the Hacktoberfest community for organizing this hackathon!
GitHub Repository: https://github.com/hira8969/E-Voting-Pilot-System-for-Local-Body-Elections
Live Demo Video / Project Link: https://lnkd.in/p/dKgcisNt
Drop a ❤️ if you found this interesting, and leave your thoughts on digital voting in the comments!
---
### 4. Optional Polish:
- **Cover Image:** You can click the **✨ Generate Image** button at the top to let DEV generate an AI cover banner for your post.
- **Preview:** Click the **Preview** tab next to "Edit" to see how clean the article looks before publishing.
- Click **Publish** (blue button at the bottom left) when you're ready!
來源:DEV Community · dev.to