Skip to content

Rob-Leggett/angular_websockets_security

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

133 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Angular WebSocket Security (Modernized)

CI Security Scan

A modern full-stack application demonstrating secure WebSocket communication with token-based authentication using Spring Boot 3.4 and Angular 19.

πŸ” Security Features

This application implements a complete token-based security model for both REST API and WebSocket connections:

REST API Security

  • 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

WebSocket Security (CRITICAL)

  • Token Validation on CONNECT - WebSocketTokenInterceptor validates 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

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     HTTP + WebSocket      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 β”‚  ─────────────────────>   β”‚                     β”‚
β”‚  Angular 19     β”‚     X-AUTH-TOKEN          β”‚  Spring Boot 3.4    β”‚
β”‚  Frontend       β”‚  <─────────────────────   β”‚  Backend            β”‚
β”‚                 β”‚                           β”‚                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                                              β”‚
        β”‚  STOMP over SockJS                          β”‚
        β”‚  X-AUTH-TOKEN in headers                    β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Technology Stack

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

πŸš€ Quick Start

Prerequisites

  • Java 21+
  • Node.js 20+
  • Maven 3.9+

Run the Backend

cd backend
mvn spring-boot:run

The backend will start on http://localhost:8080

Run the Frontend

cd frontend
npm install
npm start

The frontend will start on http://localhost:4200 with proxy to backend.

Default Login Credentials

  • Email: user@example.com
  • Password: password

πŸ”’ Security Implementation Details

Authentication Flow

  1. Client sends POST /api/authentication/login with Authorization: Basic <base64> header
  2. Server validates credentials against database (BCrypt)
  3. Server generates JWT token signed with HMAC-SHA256
  4. Server returns token in X-AUTH-TOKEN response header
  5. Client stores token in sessionStorage
  6. All subsequent requests include X-AUTH-TOKEN header

WebSocket Authentication Flow

  1. Client connects to /stomp endpoint via SockJS
  2. STOMP CONNECT frame includes X-AUTH-TOKEN in headers
  3. WebSocketTokenInterceptor extracts and validates token
  4. If valid, sets Authentication in SecurityContext
  5. Subscription requests are authorized against configured rules
  6. Only authenticated users can subscribe to /user/notifications

Key Security Classes

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

πŸ“ Project Structure

β”œβ”€β”€ 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)

πŸ§ͺ Testing

Backend Tests

cd backend
mvn test

Frontend Build

cd frontend
npm run build

πŸ“– API Endpoints

Authentication

  • POST /api/authentication/login - Login with Basic Auth
  • POST /api/authentication/logout - Logout

User

  • GET /api/user - Get current user details

Customers (Protected)

  • GET /api/customers - List all customers
  • GET /api/customers/search?query= - Search customers
  • POST /api/customers - Create customer
  • PUT /api/customers/{id} - Update customer
  • DELETE /api/customers/{id} - Delete customer

WebSocket

  • CONNECT /stomp - WebSocket endpoint (SockJS)
  • SUBSCRIBE /user/notifications - User notifications (requires auth)

πŸ”„ Migration from Original

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

Security Preserved

  • βœ… Token-based stateless authentication
  • βœ… X-AUTH-TOKEN header convention
  • βœ… WebSocket token validation on CONNECT
  • βœ… Message-level security for STOMP
  • βœ… Per-user notification subscriptions

πŸ“ License

MIT License

About

AngularJS, Websockets, Restful, Spring, Spring Security, Hibernate, Bootstrap, Maven

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors