Skip to content

This is a RESTful chat application built with Spring Boot that allows users to register, log in, and exchange messages in real-time.

License

Notifications You must be signed in to change notification settings

Ayushkhodankar/ChatApplication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat Application with Spring Boot, JWT, and Spring Security

Overview

This is a RESTful chat application built with Spring Boot that allows users to register, log in, and exchange messages in real-time. The application is secured using JWT (JSON Web Tokens) for authentication and Spring Security for authorization.

Features

  • User Registration and Login with JWT authentication.
  • Secure message exchange between users.
  • Messages are associated with sender and receiver information.
  • Spring Security handles user authentication and access control.
  • MySQL database used to store users and chat messages.

Technologies Used

  • Spring Boot - Backend framework.
  • Spring Security - For authentication and authorization.
  • JWT (JSON Web Tokens) - For stateless authentication.
  • Spring Data JPA - For database interaction.
  • MySQL - Relational database.

Getting Started

Prerequisites

Before you begin, ensure you have the following installed:

  • Java 11+
  • Maven
  • MySQL (or any relational database)

Installation and Setup

  1. Clone the repository:
    git clone https://github.com/your-username/chat-application.git
  2. Navigate to the project directory:
    cd chat-application
  3. Open src/main/resources/application.properties and configure your database:
    
    spring.datasource.url=jdbc:mysql://localhost:3306/chatdb
    spring.datasource.username=root
    spring.datasource.password=yourpassword
    spring.jpa.hibernate.ddl-auto=update
    jwt.secret=mysecretkey
            
  4. Create the database in MySQL:
    CREATE DATABASE chatdb;
  5. Build and run the application:
    mvn spring-boot:run

Running the Application

Once the application is running, it will be available on http://localhost:8080.

API Endpoints

  • POST /auth/register - Register a new user.
  • POST /auth/login - Authenticate a user and get a JWT.
  • POST /chat/send - Send a message to another user (requires JWT).
  • GET /chat/messages - Retrieve chat history for the authenticated user (requires JWT).

Sample JSON for Registration


{
  "username": "john",
  "password": "password123"
}

Sample JSON for Sending a Message


{
  "content": "Hello, how are you?",
  "receiverUsername": "alice"
}

Authentication with JWT

When a user successfully logs in using the /auth/login endpoint, the server responds with a JWT. This token should be included in the header of every subsequent request to secure endpoints (e.g., sending or fetching messages).

For example, add the following header:

Authorization: Bearer your-jwt-token

Architecture

The chat application follows a layered architecture:

  • Controller Layer: Handles HTTP requests and responses.
  • Service Layer: Contains the business logic and interacts with repositories.
  • Repository Layer: Manages data persistence using JPA and interacts with the database.

Database Schema

The application uses two tables:

  1. Users:
    • id: Auto-generated user ID.
    • username: Unique username.
    • password: Encrypted user password.
  2. Chat Messages:
    • id: Auto-generated message ID.
    • content: Text content of the message.
    • sender_id: ID of the user who sent the message.
    • receiver_id: ID of the user who received the message.
    • timestamp: When the message was sent.

Security

The application uses Spring Security to secure user registration, login, and messaging functionalities:

  • All endpoints, except for /auth/register and /auth/login, are secured and require a valid JWT token.
  • Passwords are stored in an encrypted format using BCrypt.
  • JWT tokens are used to authorize users for accessing protected endpoints.

Testing the API

You can test the API using tools like Postman or cURL. Here's an example of using cURL to test the login functionality:


curl -X POST http://localhost:8080/auth/login -H "Content-Type: application/json" -d '{
  "username": "john",
  "password": "password123"
}'

This will return a JWT token which can then be used to access the secure /chat/send and /chat/messages endpoints.

Contributing

If you'd like to contribute to the project, feel free to submit a pull request or open an issue on the GitHub repository. All contributions are welcome!

License

This project is licensed under the MIT License.

About

This is a RESTful chat application built with Spring Boot that allows users to register, log in, and exchange messages in real-time.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages