This project demonstrates a secure WebSocket communication setup between a Spring Boot backend and a FastAPI server, leveraging JWT (JSON Web Tokens) for authentication and message encryption.
This application facilitates real-time messaging between Java (Spring Boot) and Python (FastAPI) applications using WebSockets. It showcases:
JWT Implementation: Utilizes JWT tokens with HS256 algorithm for secure message encoding and decoding.
WebSocket Communication: Establishes a bidirectional communication channel where Spring Boot sends encrypted messages as JWT tokens to FastAPI.
Authentication: Ensures secure message transfer by verifying JWT tokens on FastAPI using a shared secret key.
Generates JWT tokens containing message payloads.
Establishes WebSocket connections with FastAPI.
Sends JWT tokens over WebSocket for real-time communication.
Runs WebSocket endpoints to receive encrypted JWT tokens.
Decodes and verifies JWT tokens to extract message payloads.
Sends decoded messages back to Spring Boot for confirmation.
JWT Tokens: Signed with a shared secret key (HS256) for secure encoding and decoding of messages.
Message Integrity: Ensures that messages remain confidential and tamper-proof during transmission over WebSocket.
WS_JWT/
├── spring-boot/
│ ├── src/
│ │ ├── main/
│ │ │ ├── java/
│ │ │ │ └── com/
│ │ │ │ └── example/
│ │ │ │ └── jwtwebsocket/
│ │ │ │ ├── controller/
│ │ │ │ │ └── MessageController.java
│ │ │ │ ├── service/
│ │ │ │ │ └── JwtService.java
│ │ │ │ ├── model/
│ │ │ │ │ └── Message.java
│ │ │ │ └── JwtWebsocketApplication.java
│ │ │ └── resources/
│ │ │ └── application.properties
│ │ └── pom.xml
└ ── fastapi/
├── main.py
├── jwt_service.py
-
MessageController.java
Handles incoming requests and sends JWT tokens over WebSocket.
-
JwtService.java
Provides JWT token generation and verification services.
-
Message.java
Defines the data model for messages exchanged over WebSocket.
-
JwtWebsocketApplication.java
Main entry point for the Spring Boot application.
-
application.properties
Configuration properties for the Spring Boot application.
-
pom.xml
Maven configuration file for managing dependencies and build process.
-
main.py
Defines WebSocket endpoint and JWT token decoding logic in FastAPI.
-
jwt_service.py
Provides functions for decoding JWT tokens in FastAPI.
Java Development Kit (JDK)
Maven
Python 3.x
FastAPI
Uvicorn
This project is licensed under the MIT License.