A modern full-stack application demonstrating secure WebSocket communication with token-based authentication using Spring Boot 3.4 and Angular 19.
This application implements a complete token-based security model for both REST API and WebSocket connections:
- JWT Token Authentication - Stateless, token-based auth
- X-AUTH-TOKEN Header - Custom header for token transport
- BCrypt Password Encoding - Secure password storage
- CORS Configuration - Controlled cross-origin access
- Token Validation on CONNECT -
WebSocketTokenInterceptorvalidates JWT tokens passed in STOMP headers - Message-Level Security - CONNECT, MESSAGE, SUBSCRIBE require authentication
- SecurityContext Integration - Authenticated user available in WebSocket handlers
- User-Specific Subscriptions - Secure per-user notification channels
┌─────────────────┐ HTTP + WebSocket ┌─────────────────────┐
│ │ ─────────────────────> │ │
│ Angular 19 │ X-AUTH-TOKEN │ Spring Boot 3.4 │
│ Frontend │ <───────────────────── │ Backend │
│ │ │ │
└─────────────────┘ └─────────────────────┘
│ │
│ STOMP over SockJS │
│ X-AUTH-TOKEN in headers │
└──────────────────────────────────────────────┘
| Component | Version |
|---|---|
| Java | 21 LTS |
| Spring Boot | 3.4.1 |
| Spring Security | 6.4.x |
| Angular | 19.x |
| Node.js | 20+ LTS |
| JWT (jjwt) | 0.12.6 |
| H2 Database | In-memory |
- Java 21+
- Node.js 20+
- Maven 3.9+
cd backend
mvn spring-boot:runThe backend will start on http://localhost:8080
cd frontend
npm install
npm startThe frontend will start on http://localhost:4200 with proxy to backend.
- Email:
user@example.com - Password:
password
- Client sends
POST /api/authentication/loginwithAuthorization: Basic <base64>header - Server validates credentials against database (BCrypt)
- Server generates JWT token signed with HMAC-SHA256
- Server returns token in
X-AUTH-TOKENresponse header - Client stores token in sessionStorage
- All subsequent requests include
X-AUTH-TOKENheader
- Client connects to
/stompendpoint via SockJS - STOMP CONNECT frame includes
X-AUTH-TOKENin headers WebSocketTokenInterceptorextracts and validates token- If valid, sets
AuthenticationinSecurityContext - Subscription requests are authorized against configured rules
- Only authenticated users can subscribe to
/user/notifications
| Class | Purpose |
|---|---|
JwtTokenProvider |
Creates and validates JWT tokens |
JwtAuthenticationFilter |
Validates tokens on HTTP requests |
WebSocketTokenInterceptor |
Validates tokens on WebSocket CONNECT |
WebSocketSecurityConfig |
Configures message-level security rules |
SecurityConfig |
Main Spring Security configuration |
├── backend/ # Spring Boot application
│ ├── src/main/java/au/com/example/
│ │ ├── config/ # Configuration classes
│ │ ├── controller/ # REST and WebSocket controllers
│ │ ├── model/ # JPA entities
│ │ ├── repository/ # Spring Data repositories
│ │ └── security/ # Security components
│ └── src/main/resources/
│ ├── application.yml # Application configuration
│ └── data.sql # Initial data
│
├── frontend/ # Angular application
│ └── src/app/
│ ├── core/
│ │ ├── auth/ # Authentication service & guard
│ │ ├── interceptors/ # HTTP interceptor
│ │ └── websocket/ # WebSocket service
│ └── features/ # Feature components
│
└── (legacy modules) # Original AngularJS code (deprecated)
cd backend
mvn testcd frontend
npm run buildPOST /api/authentication/login- Login with Basic AuthPOST /api/authentication/logout- Logout
GET /api/user- Get current user details
GET /api/customers- List all customersGET /api/customers/search?query=- Search customersPOST /api/customers- Create customerPUT /api/customers/{id}- Update customerDELETE /api/customers/{id}- Delete customer
CONNECT /stomp- WebSocket endpoint (SockJS)SUBSCRIBE /user/notifications- User notifications (requires auth)
This is a complete rewrite of the original AngularJS + Spring 4 application:
| Original | Modernized |
|---|---|
| AngularJS 1.4 | Angular 19 |
| Spring 4 + Spring Security 4 | Spring Boot 3.4 + Spring Security 6 |
| Gulp 3 | Angular CLI |
| Custom HMAC tokens | JWT (jjwt) |
| WAR deployment | Embedded Tomcat JAR |
| javax.* APIs | jakarta.* APIs |
- ✅ Token-based stateless authentication
- ✅ X-AUTH-TOKEN header convention
- ✅ WebSocket token validation on CONNECT
- ✅ Message-level security for STOMP
- ✅ Per-user notification subscriptions
MIT License