A modern Spring Boot application for managing books and reviews with JWT authentication, role-based authorization, and complete CRUD operations.
- π JWT Authentication - Secure token-based authentication
- π‘οΈ Role-Based Authorization - USER, AUTHOR, and ADMIN roles
- π Password Security - BCrypt password hashing
- π Book Management - Complete CRUD operations for books
- β Review System - User reviews with 1-5 star ratings
- π― Professional APIs - RESTful endpoints with proper error handling
- ποΈ PostgreSQL Integration - Robust database management
- Backend: Spring Boot 3.5.3, Spring Security, Spring Data JPA
- Database: PostgreSQL
- Authentication: JWT (JSON Web Tokens)
- Security: BCrypt Password Encoding
- Build Tool: Maven
- Java Version: 21
Before running the application, ensure you have:
- Java 21 or higher
- Maven 3.6 or higher
- PostgreSQL 12 or higher
- Git
git clone <your-repository-url>
cd webBookCreate a PostgreSQL database:
CREATE DATABASE Book;
CREATE USER root WITH PASSWORD 'root';
GRANT ALL PRIVILEGES ON DATABASE Book TO root;Update src/main/resources/application.properties if needed:
spring.datasource.url=jdbc:postgresql://localhost:5432/Book
spring.datasource.username=root
spring.datasource.password=rootmvn clean installmvn spring-boot:runThe application will start on http://localhost:8080
| Role | Permissions |
|---|---|
| USER | Create reviews, view books and reviews |
| AUTHOR | All USER permissions + create/update/delete own books |
| ADMIN | Full access to all operations |
curl -X POST http://localhost:8080/api/users/create \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"email": "john@example.com",
"password": "securePassword123",
"role": "USER"
}'curl -X POST http://localhost:8080/api/users/login \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"password": "securePassword123"
}'Response:
{
"message": "User logged in successfully",
"statusCode": 200,
"data": {
"token": "eyJhbGciOiJIUzI1NiJ9...",
"username": "john_doe",
"email": "john@example.com",
"role": "USER",
"userId": 1
}
}For protected endpoints, include the JWT token:
Authorization: Bearer <your-jwt-token>
curl -X POST http://localhost:8080/api/books \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"genre": "Classic Literature",
"description": "A classic American novel",
"coverImageUrl": "https://example.com/cover.jpg"
}'curl -X GET http://localhost:8080/api/bookscurl -X GET http://localhost:8080/api/books/1curl -X PUT http://localhost:8080/api/books/1 \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"title": "Updated Title",
"author": "Updated Author",
"genre": "Updated Genre",
"description": "Updated description"
}'curl -X DELETE http://localhost:8080/api/books/1 \
-H "Authorization: Bearer <token>"curl -X GET http://localhost:8080/api/books/my-books \
-H "Authorization: Bearer <token>"curl -X POST http://localhost:8080/api/reviews \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"rating": 5,
"comment": "Excellent book! Highly recommended.",
"book": {"id": 1}
}'curl -X GET http://localhost:8080/api/reviewscurl -X GET http://localhost:8080/api/reviews/book/1curl -X GET http://localhost:8080/api/reviews/my-reviews \
-H "Authorization: Bearer <token>"curl -X DELETE http://localhost:8080/api/reviews/1 \
-H "Authorization: Bearer <token>"curl -X GET http://localhost:8080/api/users \
-H "Authorization: Bearer <token>"- Password Hashing: All passwords are hashed using BCrypt
- JWT Tokens: Secure token-based authentication with 24-hour expiration
- Role-Based Access: Fine-grained permissions based on user roles
- CORS Support: Configured for cross-origin requests
- Input Validation: Comprehensive validation for all inputs
id(Primary Key)username(Unique)email(Unique)password(BCrypt hashed)role(USER/AUTHOR/ADMIN)created_at,updated_at
id(Primary Key)title,author,genredescription,cover_image_urlcreated_by(Foreign Key to Users)created_at
id(Primary Key)rating(1-5)commentuser_id(Foreign Key to Users)book_id(Foreign Key to Books)created_at
- Only ADMIN and AUTHOR users can create books
- Authors can only modify their own books
- ADMIN users can modify any book
- All users can view books (public access)
- Users can only review each book once
- Rating must be between 1-5
- Users can only delete their own reviews
- ADMIN can delete any review
- All reviews are publicly viewable
The API returns consistent error responses:
{
"message": "Error description",
"statusCode": 400,
"data": null
}200- Success201- Created400- Bad Request401- Unauthorized403- Forbidden404- Not Found409- Conflict
- Create an AUTHOR user:
curl -X POST http://localhost:8080/api/users/create \
-H "Content-Type: application/json" \
-d '{"username":"author1","email":"author@example.com","password":"password123","role":"AUTHOR"}'- Login to get token:
curl -X POST http://localhost:8080/api/users/login \
-H "Content-Type: application/json" \
-d '{"username":"author1","password":"password123"}'- Create a book:
curl -X POST http://localhost:8080/api/books \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"title":"My Book","author":"Author Name","genre":"Fiction","description":"A great book"}'- Create a USER and review the book:
# Create user
curl -X POST http://localhost:8080/api/users/create \
-H "Content-Type: application/json" \
-d '{"username":"user1","email":"user@example.com","password":"password123","role":"USER"}'
# Login as user
curl -X POST http://localhost:8080/api/users/login \
-H "Content-Type: application/json" \
-d '{"username":"user1","password":"password123"}'
# Create review
curl -X POST http://localhost:8080/api/reviews \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"rating":5,"comment":"Amazing book!","book":{"id":1}}'If you have docker-compose.yml in your project, you can run:
docker-compose up -djwt.secret=BookNestSecretKeyForJWTTokenGenerationAndValidation123456789
jwt.expiration=86400000spring.datasource.url=jdbc:postgresql://localhost:5432/Book
spring.datasource.username=root
spring.datasource.password=root- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the application logs for error details
- Ensure your PostgreSQL database is running
- Verify your JWT tokens are valid and not expired
- Check that you have the correct permissions for the operation
Happy Coding! π