Skip to content

A secure, role-based blogging backend built with Spring Boot, featuring JWT authentication, MapStruct, and global exception handling. Supports CRUD operations for posts, comments, and likes, with fine-grained role-based access control.

Notifications You must be signed in to change notification settings

Risspecct/Blog-Platform-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📝 Risspecct Blog Platform Backend

Java Spring Boot License Build

A secure, role-based blogging platform backend built with Spring Boot. Features include JWT authentication, user role hierarchy, and full CRUD operations for posts, comments, and likes.


🚀 Features

  • Register & login with JWT authentication

  • Secure password hashing using BCrypt

  • Role-based access: VIEWER, AUTHOR, MOD, ADMIN

  • Users can:

    • Create, update, delete their posts
    • Comment on and like posts
    • View posts, comments, and own data
  • Moderators can delete any post or comment

  • Admins can manage users (ban, delete, assign roles)

  • Global exception handling with descriptive error messages

  • Clean DTO ↔ Entity mapping using MapStruct

  • Interactive Swagger UI for API testing and documentation


📁 Project Structure

risspecct-blog-platform-backend/
├── Controllers/
├── Entities/
├── Dtos/
├── Services/
├── Repositories/
├── Filters/
├── Security/
├── Exceptions/
├── Mappers/
├── Enums/
├── postman/
├── src/main/resources/

🔐 Roles & Permissions

Role Permissions
VIEWER View posts, comment, like
AUTHOR All of the above + create/edit/delete own posts
MOD All of the above + delete any post/comment
ADMIN Full access + manage users and assign roles

📌 API Endpoint Overview

⚖️ Auth (/users)

  • POST /register – Register new user
  • POST /login – Authenticate and get JWT

👤 User (/users)

  • GET / – View own profile
  • PUT / – Update own profile
  • DELETE / – Delete own account
  • GET /comments – Get own comments
  • GET /all – (Admin) View all users

📄 Posts (/posts)

  • POST / – (Author) Add post
  • GET /{id} – View specific post
  • PUT /{id} – (Author) Update own post
  • DELETE /{id} – (Author) Delete own post
  • GET /users/me/posts – View own posts
  • GET /users/{userId}/posts – View posts by user

💬 Comments (/posts/{postId}/comments)

  • POST / – Add comment
  • GET / – Get all comments on a post
  • GET /{id} – Get single comment
  • PUT /{id} – Update own comment
  • DELETE /{id} – Delete own comment

❤️ Likes (/posts/{postId})

  • POST /like – Like a post
  • GET /likes – Get like count
  • DELETE /like – Remove like

🛡️ Admin (/admin)

  • PUT /users/roles/{userId} – Assign roles
  • PUT /users/ban/{userId} – Ban/unban user
  • DELETE /users/delete/{userId} – Delete user

🩰 Moderator (/mod)

  • DELETE /delete/posts/{postId} – Delete any post
  • DELETE /delete/comments/{commentId} – Delete any comment

🛡️ Security Highlights

  • Stateless JWT authentication
  • Role hierarchy: ADMIN > MOD > AUTHOR > VIEWER
  • @PreAuthorize annotations on secured routes
  • Custom exception handler for clean error responses

🌱 Tech Stack

  • Java 21
  • Spring Boot 3.4+
  • Spring Security
  • Spring Data JPA (Hibernate)
  • MySQL 8+
  • JWT (jjwt)
  • MapStruct
  • Lombok

📚 API Documentation

Use the “Authorize” button and provide Bearer <JWT> to test secured endpoints.


🛠️ Setup & Run

Prerequisites

  • Java 21+
  • Maven
  • MySQL 8+

Steps

git clone https://github.com/your-username/risspecct-blog-platform-backend.git
cd risspecct-blog-platform-backend
cp src/main/resources/application.properties.example src/main/resources/application.properties
# edit DB and JWT settings in application.properties
./mvnw spring-boot:run

🧪 Testing

This project includes a comprehensive test suite for both the service and controller layers, using:

  • JUnit 5
  • Mockito
  • Spring Security Test

All tests run against an in-memory H2 database, ensuring they are:

  • Fast
  • Isolated
  • Do not affect your local MySQL data

Test Structure

✅ Pure Unit Tests

@ExtendWith(MockitoExtension.class)
Used for service-layer testing to verify business logic without loading the Spring context.

🌐 Web Layer Slice Tests

@WebMvcTest
Used for controller-layer testing to verify:

  • Security rules (@PreAuthorize)
  • Role hierarchy
  • Request validation (@Valid)
  • Global exception handling

Running Tests

Action Command
Run all tests bash<br>./mvnw test
Build project without tests bash<br>./mvnw package -DskipTests

🔮 Postman Collection

Located at: postman/blog-platform-api.postman_collection.json

  1. Import into Postman
  2. Use /users/login to retrieve a JWT
  3. JWT auto-assigned to {{token}} for all requests

📃 License

This project is licensed under the MIT License.


🤝 Contributions

Pull requests are welcome. For major changes, please open an issue first to discuss what you’d like to change.

About

A secure, role-based blogging backend built with Spring Boot, featuring JWT authentication, MapStruct, and global exception handling. Supports CRUD operations for posts, comments, and likes, with fine-grained role-based access control.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages