A comprehensive REST API for a blogging platform built with Spring Boot, featuring user authentication, blog management, image uploads, and role-based access control.
Created by Prince Pal
-
User Authentication & Authorization
- JWT-based authentication with HTTP-only cookies
- Role-based access control (Admin, Collaborator)
- Secure password hashing with BCrypt
-
User Management
- Admin user registration with secret key
- Admin can create users with auto-generated credentials
- Profile management for all users
- User deletion (admin only)
-
Blog Management
- Full CRUD operations for blog posts
- Rich text content support
- Author-based filtering
- Timestamp tracking (created/updated)
-
Image Support
- Multiple images per blog post
- File upload with validation (JPEG, PNG, GIF, WebP)
- Image ordering and management
- Automatic cleanup on blog deletion
- Static file serving
- JWT token authentication
- Role-based permissions
- Input validation and sanitization
- Secure file upload handling
- CSRF protection disabled for API
- CORS configuration ready
- Backend Framework: Spring Boot 4.0.1
- Language: Java 17
- Database: MySQL 8.0+
- ORM: Spring Data JPA with Hibernate
- Security: Spring Security with JWT
- File Upload: Spring Multipart
- Build Tool: Maven
- Documentation: Spring Boot Actuator (ready for Swagger)
- Java: JDK 17 or higher
- MySQL: 8.0 or higher
- Maven: 3.6+ (or use included Maven wrapper)
- Git: For version control
git clone <repository-url>
cd bloggerCreate a MySQL database:
CREATE DATABASE blogger_db;Update database credentials in src/main/resources/application.properties:
spring.datasource.username=your_mysql_username
spring.datasource.password=your_mysql_passwordCreate the image upload directory:
mkdir -p uploads/imagesUsing Maven wrapper (recommended):
./mvnw clean install
./mvnw spring-boot:runOr using system Maven:
mvn clean install
mvn spring-boot:runThe application will start on http://localhost:8080
Key configuration options in application.properties:
# Server
server.port=8080
# Database
spring.datasource.url=jdbc:mysql://localhost:3306/blogger_db?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=password
# JWT
spring.app.jwtSecret=your-256-bit-secret
spring.app.jwtExpirationMs=30000000
spring.app.authKey=your-admin-secret-key
# File Upload
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=50MB
app.upload.dir=uploads/images/
# Frontend (for CORS)
frontend.url=http://localhost:5173POST /api/auth/signup
Content-Type: application/json
{
"username": "admin",
"password": "securepassword",
"secretKey": "__SecretKey__"
}POST /api/auth/signin
Content-Type: application/json
{
"username": "admin",
"password": "securepassword"
}Response: Sets JWT cookie + user info
POST /api/auth/users
Authorization: Bearer {jwt-token}
Content-Type: application/json
{
"fullName": "John Doe",
"phoneNumber": "+1234567890"
}Response: Generated credentials for the new user
PUT /api/auth/users/{userId}
Authorization: Bearer {jwt-token}
Content-Type: application/json
{
"fullName": "Updated Name",
"phoneNumber": "+0987654321"
}PUT /api/auth/profile
Authorization: Bearer {jwt-token}
Content-Type: application/json
{
"fullName": "My Updated Name",
"phoneNumber": "+0987654321"
}DELETE /api/auth/users/{userId}
Authorization: Bearer {jwt-token}POST /api/auth/signoutResponse: Clears JWT cookie
POST /api/blogs
Authorization: Bearer {jwt-token}
Content-Type: application/json
{
"title": "My First Blog Post",
"description": "This is a comprehensive blog post content..."
}GET /api/blogsGET /api/blogs/{id}GET /api/blogs/author/{authorId}GET /api/blogs/my
Authorization: Bearer {jwt-token}PUT /api/blogs/{id}
Authorization: Bearer {jwt-token}
Content-Type: application/json
{
"title": "Updated Blog Title",
"description": "Updated content..."
}DELETE /api/blogs/{id}
Authorization: Bearer {jwt-token}POST /api/blogs/{blogId}/images
Authorization: Bearer {jwt-token}
Content-Type: multipart/form-data
file: [image file]
displayOrder: 1POST /api/blogs/{blogId}/images/multiple
Authorization: Bearer {jwt-token}
Content-Type: multipart/form-data
files: [image1.jpg, image2.png]
displayOrders: [1, 2]GET /api/blogs/{blogId}/imagesGET /api/blogs/images/{imageId}PUT /api/blogs/images/{imageId}/order?displayOrder=2
Authorization: Bearer {jwt-token}DELETE /api/blogs/images/{imageId}
Authorization: Bearer {jwt-token}users- User accounts and profilesblogs- Blog posts with author relationshipsimages- Image metadata with blog relationships
- User (1) β Blog (Many)
- Blog (1) β Image (Many)
- ROLE_ADMIN: Full access to all features
- ROLE_COLLABORATOR: Blog and image management
| Feature | Admin | Collaborator |
|---|---|---|
| Create Users | β | β |
| Delete Users | β | β |
| Manage All Blogs | β | β |
| Manage Own Blogs | β | β |
| Upload Images | β | β |
| View All Content | β | β |
blogger/
βββ src/main/java/com/princeworks/blogger/
β βββ BloggerApplication.java # Main application class
β βββ config/ # Configuration classes
β β βββ AppConfig.java # ModelMapper bean
β β βββ WebMvcConfig.java # Static resource config
β βββ controller/ # REST controllers
β β βββ AuthController.java # Authentication & user management
β β βββ BlogController.java # Blog & image operations
β βββ exceptions/ # Custom exceptions
β βββ model/ # JPA entities
β β βββ User.java # User entity
β β βββ Blog.java # Blog entity
β β βββ Image.java # Image entity
β β βββ AppRole.java # Role enum
β βββ payload/ # DTOs
β βββ repositories/ # Data access layer
β βββ security/ # Security configuration
β β βββ jwt/ # JWT utilities
β β βββ request/ # Request DTOs
β β βββ response/ # Response DTOs
β β βββ services/ # User details services
β β βββ WebSecurityConfig.java # Security config
β βββ service/ # Business logic
β βββ util/ # Utility classes
βββ src/main/resources/
β βββ application.properties # Configuration
β βββ static/ # Static resources
βββ src/test/ # Test classes
βββ uploads/images/ # Image storage (create manually)
βββ pom.xml # Maven configuration
βββ README.md # This file
./mvnw testUse tools like Postman, Insomnia, or curl to test endpoints. Import the following collection structure:
-
Authentication Flow
- Register admin β Login β Get JWT token
- Create users β Test role-based access
-
Blog Management
- Create blog β Upload images β Update content β Delete
- Update database credentials
- Configure JWT secret key
- Set up file storage permissions
- Configure CORS for frontend domain
- Set up reverse proxy (nginx/apache)
- Configure SSL certificates
- Set up log rotation
- Configure backup strategy
FROM openjdk:17-jdk-slim
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow Java naming conventions
- Add unit tests for new features
- Update documentation
- Ensure all tests pass
- Use meaningful commit messages
This project is licensed under the MIT License - see the LICENSE file for details.
For support, contact Prince Pal or create an issue in the repository.
Prince Pal - Project Creator & Developer
- Spring Boot team for the excellent framework
- JWT.io for JWT implementation guidance
- MySQL team for the database
- All contributors and the open-source community
Happy Blogging! π