Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

21 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Spring Boot Starter Kit

Java Spring Boot License: MIT Docker CI/CD

Stop copy-pasting boilerplate. Clone this, rename the package, and ship.

A production-ready Spring Boot 3 REST API template with everything you need baked in โ€” JWT auth, rate limiting, Swagger docs, Docker, GitHub Actions CI/CD, and more.


โœจ Features

Feature Details
๐Ÿ” JWT Authentication Access token (15 min) + Refresh token (7 days) with rotation
๐Ÿ‘ฎ Role-Based Access Control ROLE_USER and ROLE_ADMIN via @PreAuthorize
๐Ÿšฆ Rate Limiting Per-IP rate limiting using Bucket4j (configurable)
๐Ÿ“„ OpenAPI 3 / Swagger UI Auto-generated docs at /swagger-ui.html
๐Ÿ›ก๏ธ Global Exception Handling Consistent ApiResponse<T> across all endpoints
๐Ÿ—ƒ๏ธ Flyway Migrations Version-controlled schema with rollback support
๐Ÿณ Docker + Docker Compose Multi-stage build, nginx reverse proxy, MySQL
โš™๏ธ CI/CD via GitHub Actions Test โ†’ Build โ†’ Push Docker โ†’ Deploy to EC2
๐Ÿ“Š Actuator /actuator/health, /actuator/metrics
๐Ÿ” Request ID Tracking MDC-based request ID in every log line
๐ŸŒ CORS Configured Configurable via environment variables
โœ… Bean Validation Jakarta Validation on all request DTOs

๐Ÿ“ Project Structure

src/main/java/com/starterkit/
โ”œโ”€โ”€ config/              # Security, Swagger, DataInitializer
โ”œโ”€โ”€ controller/          # AuthController, UserController, HealthController
โ”œโ”€โ”€ dto/
โ”‚   โ”œโ”€โ”€ request/         # RegisterRequest, LoginRequest, RefreshTokenRequest
โ”‚   โ””โ”€โ”€ response/        # ApiResponse<T>, AuthResponse, UserResponse
โ”œโ”€โ”€ entity/              # User, Role, RefreshToken
โ”œโ”€โ”€ exception/           # GlobalExceptionHandler + custom exceptions
โ”œโ”€โ”€ filter/              # JwtAuthFilter, RateLimitFilter, RequestIdFilter
โ”œโ”€โ”€ repository/          # JPA repositories
โ”œโ”€โ”€ security/            # JwtService, UserDetailsServiceImpl
โ””โ”€โ”€ service/             # AuthService, UserService

๐Ÿš€ Quick Start

Prerequisites

  • Java 17+
  • Maven 3.8+
  • Docker & Docker Compose

Option 1 โ€” Run locally (H2 in-memory, zero setup)

git clone https://github.com/raahulllkushwaha/springboot-starter-kit.git
cd springboot-starter-kit

mvn spring-boot:run

App starts at http://localhost:8080
Swagger UI: http://localhost:8080/swagger-ui.html
H2 Console: http://localhost:8080/h2-console (JDBC URL: jdbc:h2:mem:starterkit)

Option 2 โ€” Docker Compose (MySQL + Nginx)

cp .env.example .env
# Edit .env with your values

docker compose up -d

๐Ÿ”‘ API Endpoints

Auth (Public)

POST /api/v1/auth/register    โ†’ Register new user
POST /api/v1/auth/login       โ†’ Login, get tokens
POST /api/v1/auth/refresh     โ†’ Refresh access token
POST /api/v1/auth/logout      โ†’ Revoke refresh token

Users (Protected)

GET  /api/v1/users/me         โ†’ Get current user (any authenticated)
GET  /api/v1/users/{id}       โ†’ Get user by ID (admin or self)
GET  /api/v1/users            โ†’ List all users (admin only)
DELETE /api/v1/users/{id}     โ†’ Delete user (admin only)

Other

GET /api/v1/ping              โ†’ Health check
GET /actuator/health          โ†’ Spring Actuator health

๐Ÿ” Authentication Flow

1. Register  โ†’  POST /api/v1/auth/register
               Body: { name, email, password }

2. Login     โ†’  POST /api/v1/auth/login
               Body: { email, password }
               Returns: { accessToken, refreshToken, ... }

3. Use API   โ†’  Add header: Authorization: Bearer <accessToken>

4. Refresh   โ†’  POST /api/v1/auth/refresh
               Body: { refreshToken }
               Returns: new accessToken + rotated refreshToken

5. Logout    โ†’  POST /api/v1/auth/logout
               Body: { refreshToken }

Default admin credentials (seeded on startup):

  • Email: admin@example.com
  • Password: Admin@1234

โš ๏ธ Change these via environment variables ADMIN_EMAIL and ADMIN_PASSWORD before deploying.


โš™๏ธ Configuration

All key settings are environment-variable driven:

Variable Default Description
SPRING_PROFILES_ACTIVE dev dev (H2) or prod (MySQL)
DB_HOST localhost MySQL host
DB_NAME starterkit Database name
DB_USERNAME โ€” MySQL username
DB_PASSWORD โ€” MySQL password
JWT_SECRET (dev default) Must change in prod
JWT_ACCESS_EXPIRY 900000 Access token TTL (ms)
JWT_REFRESH_EXPIRY 604800000 Refresh token TTL (ms)
CORS_ALLOWED_ORIGINS localhost:3000 Comma-separated origins

๐Ÿณ Docker

Build image manually

docker build -t springboot-starter-kit .

Run with Docker Compose

# Production (MySQL + Nginx + App)
docker compose up -d

# Development (only MySQL, run app locally)
docker compose -f docker-compose.dev.yml up -d

๐Ÿ”„ CI/CD (GitHub Actions)

The pipeline runs on every push to main:

Push to main
    โ†“
[Test] โ†’ mvn test (with MySQL service container)
    โ†“
[Docker] โ†’ Build & push image to Docker Hub
    โ†“
[Deploy] โ†’ SSH into EC2, docker compose pull && up

Required GitHub Secrets

Secret Description
DOCKERHUB_USERNAME Docker Hub username
DOCKERHUB_TOKEN Docker Hub access token
EC2_HOST EC2 public IP / domain
EC2_USER EC2 SSH username (e.g. ubuntu)
EC2_SSH_KEY EC2 private key (PEM contents)

๐Ÿ›ก๏ธ Rate Limiting

Configured in application.yml:

app:
  rate-limit:
    enabled: true
    capacity: 20          # max tokens in bucket
    refill-tokens: 20     # tokens added per window
    refill-seconds: 60    # window size

Returns 429 Too Many Requests when limit exceeded.


๐Ÿ—๏ธ Customising for Your Project

  1. Rename package: Find & replace com.starterkit โ†’ com.yourcompany.yourapp
  2. Update application.yml: Change spring.application.name
  3. Update SwaggerConfig.java: Set your name/contact/GitHub URL
  4. Update docker-compose.yml: Change container names and image names
  5. Add your entities: Create entities โ†’ migration SQL โ†’ repository โ†’ service โ†’ controller
  6. Deploy: Push to main, GitHub Actions handles the rest

๐Ÿงช Running Tests

# Unit + integration tests (uses H2)
mvn test

# Skip tests (for fast build)
mvn package -DskipTests

๐Ÿ”€ Two Auth Approaches

This starter supports two authentication strategies.

Approach 1: JWT (default โ€” main branch)

POST /login โ†’ accessToken + refreshToken returned
Client: Authorization: Bearer <token> OR HTTP-only cookie
Server: validates JWT signature โ€” zero DB calls

โœ… Stateless, mobile-friendly, microservices-ready โš ๏ธ Token revocation requires refresh token invalidation

Approach 2: Spring Session + JDBC (feature/spring-session branch)

POST /login โ†’ SESSION cookie set automatically
Client: cookie on every request (browser handles it)
Server: reads SecurityContext from JDBC session store

โœ… No custom JWT filter โ€” uses Spring Security built-in flow โœ… Instant revocation (delete session from DB) โœ… Survives server restarts (JDBC-backed) โš ๏ธ Not ideal for mobile/API clients

Switch to Spring Session

git checkout feature/spring-session
# Set in .env: SPRING_PROFILES_ACTIVE=session
mvn spring-boot:run

๐Ÿ“ฆ Tech Stack

  • Java 21 + Spring Boot 3.3.0
  • Spring Security 6 (JWT, RBAC)
  • jjwt 0.12 (JWT library)
  • Bucket4j 8 (Rate limiting)
  • SpringDoc OpenAPI 3 / Swagger UI
  • Flyway (Database migrations)
  • MySQL 8 / H2 (dev)
  • Lombok
  • Docker + Nginx
  • GitHub Actions

๐Ÿค Contributing

PRs are welcome! If you find this useful, please โญ the repo.

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

๐Ÿ“œ License

MIT ยฉ Rahul Kushwaha


If this saved you hours of setup, consider giving it a โญ

About

๐Ÿš€ Production-ready Spring Boot 3 starter โ€” JWT, Rate Limiting, Swagger, Docker, CI/CD Public โœ…

Topics

Resources

Stars

21 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages