Skip to content

Microservice auth system: JWT authentication, email verification, password reset, Grafana+Loki monitoring | Spring Boot 3.4 + Spring Cloud 2024

License

Notifications You must be signed in to change notification settings

saidyanak/microservice-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” Microservice Auth System

Java Spring Boot Spring Cloud License

Production-ready microservice authentication system with JWT, Email verification, Monitoring (Grafana + Loki), and more. Clone and customize for your own projects!

✨ Features

  • πŸ” JWT Authentication - Secure access & refresh token system
  • πŸ“§ Email Verification - Registration email verification flow
  • πŸ”‘ Password Reset - Forgot password with email link
  • πŸ‘₯ Role-Based Access - ADMIN / USER roles (easily extensible)
  • 🌐 API Gateway - Single entry point with JWT validation
  • πŸ“Š Monitoring Stack - Grafana + Loki + Promtail for log visualization
  • πŸ“¬ Mail Service - Async email sending via RabbitMQ
  • πŸ” Service Discovery - Netflix Eureka for service registration
  • πŸ“ Swagger UI - Interactive API documentation
  • 🐳 Docker Ready - PostgreSQL, RabbitMQ, MailHog containers

πŸ— Architecture

                                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                    β”‚   Your Frontend β”‚
                                    β”‚  (React/Vue/etc)β”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                             β”‚
                                             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                            API Gateway (8080)                                β”‚
β”‚                    β€’ JWT Validation β€’ Rate Limiting β€’ Routing                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”‚
                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                 β”‚                    β”‚                    β”‚
                 β–Ό                    β–Ό                    β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚   Auth Service     β”‚ β”‚   Mail Service   β”‚ β”‚  Your Services   β”‚
    β”‚      (8081)        β”‚ β”‚     (8082)       β”‚ β”‚    (Add here)    β”‚
    β”‚                    β”‚ β”‚                  β”‚ β”‚                  β”‚
    β”‚ β€’ Register/Login   β”‚ β”‚ β€’ Welcome Email  β”‚ β”‚ β€’ Custom logic   β”‚
    β”‚ β€’ JWT Generation   β”‚ β”‚ β€’ Password Reset β”‚ β”‚ β€’ Business APIs  β”‚
    β”‚ β€’ Password Reset   β”‚ β”‚ β€’ Notifications  β”‚ β”‚                  β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚                     β”‚
              β”‚     RabbitMQ        β”‚
              β–Ό     (Events)        β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚   PostgreSQL     β”‚  β”‚    MailHog       β”‚
    β”‚   (Auth DB)      β”‚  β”‚  (Dev SMTP)      β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Monitoring Stack                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚ Promtail │───▢│   Loki   │───▢│     Grafana      β”‚           β”‚
β”‚  β”‚(Collector)β”‚   β”‚(Storage) β”‚    β”‚  (Visualization) β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚   localhost:3001 β”‚           β”‚
β”‚                                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   Discovery Server       β”‚
                    β”‚   (Eureka - 8761)        β”‚
                    β”‚   Service Registry       β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

Only Docker & Docker Compose required! ✨

  • Docker 20.10+
  • Docker Compose 2.0+

No Java or Maven installation needed - everything runs in Docker containers!

1. Clone & Start

git clone https://github.com/YOUR_USERNAME/microservice-auth.git
cd microservice-auth

# Start EVERYTHING with one command (infrastructure + all services + monitoring)
make start

That's it! The first run will take ~5-10 minutes to build all Docker images and start services.

2. Alternative: Step-by-Step Start

# Start only infrastructure (DB, RabbitMQ, Redis, MailHog)
make start-infra

# Build all service Docker images
make build

# Start all microservices
make start

# Start monitoring stack
make start-monitoring

3. Access Points

Service URL Description
API Gateway http://localhost:8080 Main API endpoint
Swagger UI http://localhost:8080/swagger-ui.html API Documentation
Eureka Dashboard http://localhost:8761 Service Registry
Grafana http://localhost:3001 Log Visualization (admin/admin123)
MailHog http://localhost:8025 Email Testing UI
RabbitMQ http://localhost:15672 Message Queue (guest/guest)

πŸ“– API Endpoints

Authentication

Method Endpoint Description
POST /api/v1/auth/register Register new user
POST /api/v1/auth/login Login and get tokens
POST /api/v1/auth/refresh Refresh access token
POST /api/v1/auth/logout Invalidate refresh token
GET /api/v1/auth/verify-email?token=xxx Verify email address
POST /api/v1/auth/forgot-password Request password reset
POST /api/v1/auth/reset-password Reset password with token
GET /api/v1/auth/me Get current user info

Example: Register

curl -X POST http://localhost:8080/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123",
    "firstName": "John",
    "lastName": "Doe"
  }'

Example: Login

curl -X POST http://localhost:8080/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123"
  }'

πŸ‘₯ Roles

By default, the system includes two roles:

public enum Role {
    ADMIN,  // System administrators with full access
    USER    // Regular users with standard access
}

Extending Roles

To add custom roles, edit common/src/main/java/.../enums/Role.java:

public enum Role {
    ADMIN,
    USER,
    MODERATOR,  // Add your custom roles
    PREMIUM_USER
}

πŸ”§ Configuration

JWT Settings (auth-service/application.yml)

jwt:
  secret: your-256-bit-secret-key
  access-token-expiration: 900000    # 15 minutes
  refresh-token-expiration: 604800000 # 7 days

Database (auth-service/application.yml)

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/auth_db
    username: postgres
    password: postgres

πŸ“ Project Structure

microservice-auth/
β”œβ”€β”€ api-gateway/          # Spring Cloud Gateway
β”œβ”€β”€ auth-service/         # Authentication service
β”œβ”€β”€ mail-service/         # Email notification service
β”œβ”€β”€ discovery-server/     # Netflix Eureka
β”œβ”€β”€ common/               # Shared DTOs, Events, Enums
β”œβ”€β”€ monitoring/           # Grafana, Loki, Promtail configs
β”œβ”€β”€ docker-compose.dev.yml
β”œβ”€β”€ Makefile              # Convenient commands
└── README.md

πŸ›  Make Commands

# Main Commands
make help              # Show all available commands
make start             # Start everything (infra + services)
make stop              # Stop all services
make stop-all          # Stop everything (services + infra + monitoring)
make restart           # Restart all services
make status            # Check service health status

# Build Commands
make build             # Build all Docker images
make rebuild           # Rebuild without cache
make clean             # Clean all containers, images, volumes

# Infrastructure
make start-infra       # Start DB, RabbitMQ, Redis, MailHog
make stop-infra        # Stop infrastructure

# Monitoring
make start-monitoring  # Start Grafana + Loki
make stop-monitoring   # Stop monitoring stack

# Logs
make logs              # View all container logs
make logs-auth         # Follow Auth Service logs
make logs-mail         # Follow Mail Service logs
make logs-discovery    # Follow Discovery Server logs
make logs-gateway      # Follow API Gateway logs
make logs-errors       # View error logs

πŸ†• Adding New Services

Quick Guide

Let's say you want to add a notification-service that uses the common module:

1. Create Module Structure

mkdir notification-service
mkdir -p notification-service/src/main/java/com/thebuilders/notification
mkdir -p notification-service/src/main/resources

2. Create notification-service/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.microservice</groupId>
        <artifactId>microservice-auth</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>notification-service</artifactId>
    <name>Notification Service</name>

    <dependencies>
        <!-- Spring Boot dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Common module (if needed) -->
        <dependency>
            <groupId>com.microservice</groupId>
            <artifactId>common</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>  <!-- IMPORTANT! -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3. Update Root pom.xml

Add your module to the <modules> section:

<modules>
    <module>discovery-server</module>
    <module>api-gateway</module>
    <module>auth-service</module>
    <module>mail-service</module>
    <module>common</module>
    <module>notification-service</module>  <!-- NEW -->
</modules>

4. Create notification-service/Dockerfile

If your service uses common module:

# Build stage
FROM eclipse-temurin:21-jdk-alpine AS builder
WORKDIR /build

# Copy all POM files (required for Maven reactor)
COPY pom.xml .
COPY discovery-server/pom.xml ./discovery-server/
COPY api-gateway/pom.xml ./api-gateway/
COPY auth-service/pom.xml ./auth-service/
COPY mail-service/pom.xml ./mail-service/
COPY notification-service/pom.xml ./notification-service/
COPY common/pom.xml ./common/

# Copy source code (common + your service)
COPY common/src ./common/src
COPY notification-service/src ./notification-service/src

# Build
RUN apk add --no-cache maven && \
    mvn -pl common,notification-service -am clean package -DskipTests && \
    apk del maven

# Runtime stage
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY --from=builder /build/notification-service/target/*.jar app.jar
RUN mkdir -p /app/logs

EXPOSE 8083

HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:8083/actuator/health || exit 1

ENTRYPOINT ["java", "-jar", "app.jar"]

If your service does NOT use common:

# Build stage
FROM eclipse-temurin:21-jdk-alpine AS builder
WORKDIR /build

# Copy all POM files
COPY pom.xml .
COPY discovery-server/pom.xml ./discovery-server/
COPY api-gateway/pom.xml ./api-gateway/
COPY auth-service/pom.xml ./auth-service/
COPY mail-service/pom.xml ./mail-service/
COPY notification-service/pom.xml ./notification-service/
COPY common/pom.xml ./common/

# Copy only your service source
COPY notification-service/src ./notification-service/src

# Build (no common needed)
RUN apk add --no-cache maven && \
    mvn -pl notification-service -am clean package -DskipTests && \
    apk del maven

# Runtime stage (same as above)

5. Add to docker-compose.yml

  notification-service:
    build:
      context: .
      dockerfile: notification-service/Dockerfile
    container_name: notification-service
    ports:
      - "8083:8083"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://discovery-server:8761/eureka/
      - LOG_PATH=/app/logs
    volumes:
      - ./logs:/app/logs
    networks:
      - career-portal-network
    depends_on:
      discovery-server:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:8083/actuator/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

6. Update Makefile

build: ## Docker image'larΔ±nΔ± build et
	@docker-compose build discovery-server api-gateway auth-service mail-service notification-service

start: start-infra
	@docker-compose up -d discovery-server api-gateway auth-service mail-service notification-service

7. Build & Run

make build
make start

Key Points

  • Always copy ALL module POM files (Maven reactor needs them)
  • Add repackage goal to Spring Boot Maven Plugin
  • Copy common/src only if your service uses common module
  • Use mvn -pl common,your-service to build both modules together

πŸ“Š Monitoring

Grafana Dashboard

Access Grafana at http://localhost:3001 (admin/admin123)

Pre-configured dashboard shows:

  • All service logs in real-time
  • Filter by service, log level
  • Error tracking and alerts

Log Query Examples (Loki)

# All auth-service logs
{job="app-logs", filename=~".*auth-service.*"}

# Only ERROR level
{job="app-logs"} |= "ERROR"

# Specific user actions
{job="app-logs"} |~ "User registered|User logged in"

🐳 Docker Architecture

All services are containerized with multi-stage builds:

  1. Build Stage: Compiles Java code with Maven inside Docker (no local Maven needed)
  2. Runtime Stage: Lightweight JRE-only image for production
# Build all Docker images (Maven runs inside containers)
make build

# Start production stack
make start

# Or use docker-compose directly
docker-compose up -d

# Stop everything
make stop-all

Benefits

  • Zero local dependencies: Only Docker required
  • Consistent builds: Same environment for all developers
  • Small runtime images: JRE-only (no JDK/Maven in production)
  • Fast rebuilds: Docker layer caching optimizes build times

🀝 Contributing

  1. Fork the repository
  2. Create 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

πŸ“ License

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

πŸ™ Acknowledgments

  • Spring Boot & Spring Cloud teams
  • Netflix OSS (Eureka)
  • Grafana Labs (Loki, Grafana)

⭐ Star this repo if you find it useful!

About

Microservice auth system: JWT authentication, email verification, password reset, Grafana+Loki monitoring | Spring Boot 3.4 + Spring Cloud 2024

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published