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.
- 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.
- 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.
Before you begin, ensure you have the following installed:
- Java 11+
- Maven
- MySQL (or any relational database)
- Clone the repository:
git clone https://github.com/your-username/chat-application.git
- Navigate to the project directory:
cd chat-application
- 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
- Create the database in MySQL:
CREATE DATABASE chatdb;
- Build and run the application:
mvn spring-boot:run
Once the application is running, it will be available on http://localhost:8080.
- 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).
{
"username": "john",
"password": "password123"
}
{
"content": "Hello, how are you?",
"receiverUsername": "alice"
}
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
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.
The application uses two tables:
- Users:
id
: Auto-generated user ID.username
: Unique username.password
: Encrypted user password.
- 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.
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.
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.
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!
This project is licensed under the MIT License.