Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

furkankayam/spring-microservices-security-example

Open more actions menu

Repository files navigation

πŸ” Spring Microservices Security Example

⚑ Microservices Security Architecture Diagram

Architecture Diagram

🐳 Docker Services

Docker Services

πŸ“‹ Project Overview

This project demonstrates a comprehensive Spring Boot Microservices architecture with robust security implementation using Spring Security, API Gateway, and Service Discovery. The system includes JWT-based authentication, header-based service communication, and distributed authorization.

πŸ—οΈ Architecture Components

  • 🌐 API Gateway - Central entry point with routing and authentication
  • πŸ” Auth Service - JWT token generation and validation
  • πŸ‘₯ User Service - User management and profiles
  • πŸ“‹ Custom Service - Business logic with role-based access control
  • πŸ—„οΈ PostgreSQL - Persistent data storage
  • πŸ” Consul - Service discovery and configuration management

πŸ› οΈ Technologies

Java Spring Boot Spring Security Spring Cloud Gateway PostgreSQL Docker Consul Gradle OpenAPI JWT MapStruct Lombok


πŸš€ Quick Start

Prerequisites

  • Java 17+
  • Gradle 8.5+
  • Docker & Docker Compose
  • Git

πŸ“₯ Installation

# Clone the repository
git clone https://github.com/furkankayam/spring-microservices-security-example.git
cd spring-microservices-security-example

🐳 Running with Docker

# Build Docker images
gradle jibDockerBuild

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

πŸƒβ€β™‚οΈ Running Locally

# Start Consul (required for service discovery)
docker run -d -p 8500:8500 --name consul consul:latest

# Start PostgreSQL
docker run -d -p 5432:5432 --name postgres \
  -e POSTGRES_DB=user_db \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=postgres \
  postgres:15

# Run services in order
cd auth-service && ./gradlew bootRun
cd user-service && ./gradlew bootRun  
cd custom-service && ./gradlew bootRun
cd api-gateway && ./gradlew bootRun

πŸ”— API Endpoints

🌍 Public Endpoints

POST   http://localhost:8080/auth/generate-token           # Generate JWT token
POST   http://localhost:8080/users/save                    # Register new user
GET    http://localhost:8080/users/{username}/profile      # Get user profile
GET    http://localhost:8080/customs/public                # Public custom endpoint

πŸ”’ Protected Endpoints

GET    http://localhost:8080/customs/private/user          # USER role required
GET    http://localhost:8080/customs/private/admin         # ADMIN role required  
GET    http://localhost:8080/customs/private/admin-and-user # USER or ADMIN role required

πŸ”§ Configuration

πŸ—„οΈ Database Configuration

Database URL: jdbc:postgresql://localhost:5432/user_db
Username: postgres
Password: postgres

🌐 Service Ports

API Gateway:    8080
Auth Service:   8081
User Service:   8082
Custom Service: 8083
Consul:         8500
PostgreSQL:     5432

πŸ§ͺ Testing

πŸ“ Example API Calls

# 1. Register a new user
curl -X POST http://localhost:8080/users/save \
  -H "Content-Type: application/json" \
  -d '{"username": "testuser", "password": "password123", "role": "USER"}'

# 2. Generate JWT token
curl -X POST http://localhost:8080/auth/generate-token \
  -H "Content-Type: application/json" \
  -d '{"username": "testuser", "password": "password123"}'

# 3. Access protected endpoint
curl -X GET http://localhost:8080/customs/private/user \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

πŸ›‘οΈ Security Features

  • βœ… JWT Authentication - Stateless token-based auth
  • βœ… Role-Based Access Control - Method-level security with @PreAuthorize
  • βœ… API Gateway Integration - Centralized authentication
  • βœ… Header-Based Service Communication - Secure inter-service calls
  • βœ… Service Discovery - Dynamic service registration with Consul
  • βœ… CORS Configuration - Cross-origin request handling

πŸ“Š Monitoring

Health Checks

# Service health endpoints
GET http://localhost:8080/actuator/health
GET http://localhost:8081/actuator/health
GET http://localhost:8082/actuator/health
GET http://localhost:8083/actuator/health

# Consul UI
http://localhost:8500/ui

πŸ“š Project Structure

spring-microservices-security-example/
β”œβ”€β”€ api-gateway/          # Spring Cloud Gateway
β”œβ”€β”€ auth-service/         # Authentication & JWT
β”œβ”€β”€ user-service/         # User management
β”œβ”€β”€ custom-service/       # Business logic
β”œβ”€β”€ docker-compose.yml    # Multi-service setup
└── README.md

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the 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.


Created with ❀️ by Mehmet Furkan KAYA

LinkedIn GitHub

About

πŸ›‘οΈ Secure microservices platform using Spring Security and distributed authentication

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.