Skip to content

About

"I built a secured RESTful API using Spring Boot 3 with JWT-based authentication and role-based access control. It follows a layered architecture, uses Spring Security with a custom JWT filter, and is containerized with Docker with separate dev and production profiles."

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

🛡️ Product Service API

A production-ready REST API built with Spring Boot 3 featuring JWT authentication, role-based access control, and full security hardening.


🚀 Tech Stack

Layer Technology
Language Java 21
Framework Spring Boot 3.3
Security Spring Security + JWT (jjwt 0.12)
Database PostgreSQL (prod) / H2 (dev)
Build Tool Maven
Containerization Docker

✨ Features

  • ✅ JWT Authentication (login → token → access)
  • ✅ Role-based access control (ROLE_ADMIN, ROLE_USER)
  • ✅ Global exception handling with consistent error responses
  • ✅ Input validation on all request bodies
  • ✅ Structured logging (JSON in prod, readable in dev)
  • ✅ Dev / Prod profile separation
  • ✅ Health check endpoint via Spring Actuator
  • ✅ Dockerized for easy deployment

📁 Project Structure

src/
├── main/java/com/example/
│   ├── auth/               # Login endpoint
│   ├── exception/          # Global error handler
│   ├── security/           # JWT filter, service, config
│   └── user/               # User entity, repository, service
├── main/resources/
│   ├── application-dev.properties
│   └── application-prod.properties
└── test/
    ├── auth/               # Integration tests
    └── security/           # JWT unit tests

⚙️ Getting Started

Prerequisites

  • Java 21+
  • Maven 3.8+
  • PostgreSQL (for prod) or just run dev profile with H2

1. Clone the repo

git clone https://github.com/YOUR_USERNAME/product-service.git
cd product-service

2. Run in dev mode (H2 in-memory DB)

./mvnw spring-boot:run -Dspring-boot.run.profiles=dev

App starts at: http://localhost:8080


🔐 Authentication Flow

POST /auth/login  →  returns JWT token
GET  /products    →  requires Bearer token

Step 1 — Login

curl -X POST http://localhost:8080/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}'

Response:

{
  "accessToken": "eyJhbGciOiJIUzI1NiJ9...",
  "tokenType": "Bearer"
}

Step 2 — Use the token

curl http://localhost:8080/products \
  -H "Authorization: Bearer YOUR_TOKEN_HERE"

👥 Default Users (dev only)

Username Password Role
admin admin123 ADMIN + USER
user user123 USER

⚠️ These are seeded automatically in dev profile only. Never use in production.


🌍 API Endpoints

Method Endpoint Access Description
POST /auth/login Public Get JWT token
GET /products USER, ADMIN List all products
POST /products ADMIN only Create product
PUT /products/{id} ADMIN only Update product
DELETE /products/{id} ADMIN only Delete product
GET /actuator/health Public Health check

🐳 Docker

# Build image
docker build -t product-service .

# Run with environment variables
docker run -p 8080:8080 \
  -e SPRING_PROFILES_ACTIVE=prod \
  -e SPRING_DATASOURCE_URL=jdbc:postgresql://host:5432/mydb \
  -e SPRING_DATASOURCE_USERNAME=postgres \
  -e SPRING_DATASOURCE_PASSWORD=yourpassword \
  -e JWT_SECRET=your_base64_secret \
  product-service

🔒 Environment Variables (Production)

Variable Description
SPRING_DATASOURCE_URL PostgreSQL connection URL
SPRING_DATASOURCE_USERNAME DB username
SPRING_DATASOURCE_PASSWORD DB password
JWT_SECRET Base64-encoded secret (min 64 bytes)

Generate a secure JWT secret:

openssl rand -base64 64

🧪 Running Tests

./mvnw test

📄 License

MIT License — free to use and modify.

About

"I built a secured RESTful API using Spring Boot 3 with JWT-based authentication and role-based access control. It follows a layered architecture, uses Spring Security with a custom JWT filter, and is containerized with Docker with separate dev and production profiles."

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages