A robust, real-time chat application built with Spring Boot and Vanilla JavaScript. Features secure JWT authentication, persistent H2 file-based storage, and a responsive multi-page frontend.
- 🔐 Secure Authentication: User registration and login with JWT (JSON Web Tokens).
- 🛡️ Secure Sender Identification: Backend validates message senders based on their auth token (spoof-proof).
- 💾 Persistent Storage: Messages and Users are stored in an embedded H2 Database (file-based).
- 🔄 Real-Time Updates: Frontend polls for new messages every 3 seconds.
- 📱 Multi-Page UI: Dedicated Login/Register page and a clean Chat interface.
- 👤 User Context: Displays logged-in user's name and provides Logout functionality.
User-friendly authentication forms with validation.
Clean UI with "Logged in as..." indicator and real-time polling.
Seamless conversation flow between multiple users.
- Framework: Spring Boot 3
- Language: Java 17+
- Database: H2 Database (File Mode)
- Security: Spring Security + JWT (JJWT)
- Build Tool: Maven
- HTML5 & CSS3: Custom responsive styling with glassmorphism effects.
- JavaScript (ES6+):
fetchAPI for REST calls,localStoragefor token management.
server/src/main
├── java/com/example/chatbackend
│ ├── config/ # AppConfig (Bean definitions)
│ ├── controller/ # AuthController, ChatController
│ ├── model/ # User, Message Entities
│ ├── repository/ # UserRepository, MessageRepository
│ ├── security/ # JwtFilter, SecurityConfig
│ ├── service/ # AuthService, ChatService
│ └── ChatBackendApplication.java
└── resources
├── application.properties # DB & JWT Config
└── data/ # H2 Database files
client/
├── index.html # Main Chat Interface (Protected)
├── login.html # Login & Register Forms
├── script.js # Auth logic, Polling, DOM manipulation
└── style.css # Styling & Responsive Design
- JDK 17 or higher
- Maven
Navigate to the server directory and run the application:
cd server
mvn spring-boot:runThe server will start at http://localhost:8080.
Simply open the client/login.html file in your modern web browser.
- Register a new account.
- Login with your credentials.
- Start Chatting! (Open a second window/browser to test multiple users).
You can configure JWT settings and Database path in application.properties:
# JWT Configuration
jwt.secret=your_very_long_secret_key_here...
jwt.expiration=86400000
# Database
spring.datasource.url=jdbc:h2:file:./src/main/resources/data/chatdb

