Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Blogger API

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

πŸš€ Features

Core Functionality

  • 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

Security Features

  • JWT token authentication
  • Role-based permissions
  • Input validation and sanitization
  • Secure file upload handling
  • CSRF protection disabled for API
  • CORS configuration ready

πŸ› οΈ Technology Stack

  • 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)

πŸ“‹ Prerequisites

  • Java: JDK 17 or higher
  • MySQL: 8.0 or higher
  • Maven: 3.6+ (or use included Maven wrapper)
  • Git: For version control

πŸ”§ Installation & Setup

1. Clone the Repository

git clone <repository-url>
cd blogger

2. Database Setup

Create 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_password

3. File System Setup

Create the image upload directory:

mkdir -p uploads/images

4. Build and Run

Using Maven wrapper (recommended):

./mvnw clean install
./mvnw spring-boot:run

Or using system Maven:

mvn clean install
mvn spring-boot:run

The application will start on http://localhost:8080

βš™οΈ Configuration

Application Properties

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:5173

πŸ“š API Documentation

Authentication Endpoints (/api/auth)

Register Admin

POST /api/auth/signup
Content-Type: application/json

{
  "username": "admin",
  "password": "securepassword",
  "secretKey": "__SecretKey__"
}

Login

POST /api/auth/signin
Content-Type: application/json

{
  "username": "admin",
  "password": "securepassword"
}

Response: Sets JWT cookie + user info

Create User (Admin Only)

POST /api/auth/users
Authorization: Bearer {jwt-token}
Content-Type: application/json

{
  "fullName": "John Doe",
  "phoneNumber": "+1234567890"
}

Response: Generated credentials for the new user

Update User Profile (Admin)

PUT /api/auth/users/{userId}
Authorization: Bearer {jwt-token}
Content-Type: application/json

{
  "fullName": "Updated Name",
  "phoneNumber": "+0987654321"
}

Update Own Profile

PUT /api/auth/profile
Authorization: Bearer {jwt-token}
Content-Type: application/json

{
  "fullName": "My Updated Name",
  "phoneNumber": "+0987654321"
}

Delete User (Admin Only)

DELETE /api/auth/users/{userId}
Authorization: Bearer {jwt-token}

Logout

POST /api/auth/signout

Response: Clears JWT cookie

Blog Endpoints (/api/blogs)

Create Blog

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 All Blogs

GET /api/blogs

Get Blog by ID

GET /api/blogs/{id}

Get Blogs by Author

GET /api/blogs/author/{authorId}

Get My Blogs

GET /api/blogs/my
Authorization: Bearer {jwt-token}

Update Blog

PUT /api/blogs/{id}
Authorization: Bearer {jwt-token}
Content-Type: application/json

{
  "title": "Updated Blog Title",
  "description": "Updated content..."
}

Delete Blog

DELETE /api/blogs/{id}
Authorization: Bearer {jwt-token}

Image Endpoints (/api/blogs)

Upload Single Image

POST /api/blogs/{blogId}/images
Authorization: Bearer {jwt-token}
Content-Type: multipart/form-data

file: [image file]
displayOrder: 1

Upload Multiple Images

POST /api/blogs/{blogId}/images/multiple
Authorization: Bearer {jwt-token}
Content-Type: multipart/form-data

files: [image1.jpg, image2.png]
displayOrders: [1, 2]

Get Blog Images

GET /api/blogs/{blogId}/images

Get Image by ID

GET /api/blogs/images/{imageId}

Update Image Order

PUT /api/blogs/images/{imageId}/order?displayOrder=2
Authorization: Bearer {jwt-token}

Delete Image

DELETE /api/blogs/images/{imageId}
Authorization: Bearer {jwt-token}

πŸ—„οΈ Database Schema

Tables Created Automatically

  • users - User accounts and profiles
  • blogs - Blog posts with author relationships
  • images - Image metadata with blog relationships

Key Relationships

  • User (1) β†’ Blog (Many)
  • Blog (1) β†’ Image (Many)

πŸ”’ Security & Permissions

User Roles

  • ROLE_ADMIN: Full access to all features
  • ROLE_COLLABORATOR: Blog and image management

Permission Matrix

Feature Admin Collaborator
Create Users βœ… ❌
Delete Users βœ… ❌
Manage All Blogs βœ… ❌
Manage Own Blogs βœ… βœ…
Upload Images βœ… βœ…
View All Content βœ… βœ…

πŸ“ Project Structure

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

πŸ§ͺ Testing

Run Tests

./mvnw test

API Testing

Use tools like Postman, Insomnia, or curl to test endpoints. Import the following collection structure:

  1. Authentication Flow

    • Register admin β†’ Login β†’ Get JWT token
    • Create users β†’ Test role-based access
  2. Blog Management

    • Create blog β†’ Upload images β†’ Update content β†’ Delete

πŸš€ Deployment

Production Checklist

  • 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

Docker Support (Future Enhancement)

FROM openjdk:17-jdk-slim
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow Java naming conventions
  • Add unit tests for new features
  • Update documentation
  • Ensure all tests pass
  • Use meaningful commit messages

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ“ž Support

For support, contact Prince Pal or create an issue in the repository.

πŸ‘¨β€πŸ’» Author

Prince Pal - Project Creator & Developer

πŸ™ Acknowledgments

  • 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! πŸŽ‰

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages