feat: Anonymous WebSocket connections with Strategy Pattern - #101
Merged
Conversation
Introduces a flexible, extensible authentication system for WebSocket connections that supports both authenticated and anonymous users. Architecture: - Strategy Pattern: Pluggable authentication strategies - Chain of Responsibility: Tries strategies in order until one succeeds - SOLID Principles: Each strategy has a single responsibility Components: - WebSocketAuthenticationStrategy: Strategy interface - AuthenticationResult: Immutable result object - TokenAuthenticationStrategy: JWT token validation - AnonymousAuthenticationStrategy: Guest user fallback - WebSocketAuthenticationService: Orchestrates strategy chain - WebSocketAuthenticationConfig: Defines strategy order Security Features: - Rate limiting: Anonymous users limited to 10 subscriptions - Logging: Tracks authenticated vs anonymous connections with remote address - Graceful degradation: Invalid tokens fall back to anonymous - Resource protection: Prevents abuse via subscription limits Benefits: - Guest users can view public trip updates in real-time - Authenticated users have unlimited subscriptions - Easy to add new auth strategies (OAuth, API keys, etc.) - Testable: Each strategy can be unit tested independently - Maintainable: Clear separation of concerns
… in TripMapper and ThumbnailUrlService
- Log session URI and query string - Log when URI is null - Log when query is empty - Log token presence and length - Will help diagnose why authenticated users are treated as anonymous
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Guest users (not logged in) could not see real-time trip updates in the browser. The WebSocket server required authentication, causing connections to fail for anonymous users.
Solution
Implemented a Strategy Pattern for WebSocket authentication that supports both authenticated and anonymous connections with proper security controls.
Architecture
Strategy Pattern Components
Security Features
Strategy Order
Code Quality
Changes
WebSocketConnectionHandler.java- Uses strategy service instead of direct JWT validationWebSocketSessionManager.java- Adds subscription counting and rate limitingauth/WebSocketAuthenticationStrategy.java- Strategy interfaceauth/AuthenticationResult.java- Immutable result objectauth/TokenAuthenticationStrategy.java- JWT validation strategyauth/AnonymousAuthenticationStrategy.java- Anonymous fallback strategyauth/WebSocketAuthenticationService.java- Strategy orchestratorauth/WebSocketAuthenticationConfig.java- Spring bean configurationTesting
Deployment
The frontend PR (#192) must be merged and deployed alongside this change.