A modern reactive Spring Boot API Gateway that provides JWT-based authentication and authorization for microservices. Built with Spring Cloud Gateway and WebFlux for high-performance, non-blocking operations.
- JWT Authentication - Stateless authentication with access and refresh tokens
- User Management - Registration, login, and profile management
- Reactive Architecture - Built on Spring WebFlux for non-blocking I/O
- Global Exception Handling - Structured error responses across all endpoints
- Secure Password Storage - BCrypt hashing with 12 rounds
- Token Refresh Mechanism - 7-day refresh tokens for seamless user experience
- Java 21 - LTS version
- Spring Boot 3.5.7 - Framework
- Spring Cloud Gateway - Reactive gateway
- Spring Security - Authentication & authorization
- PostgreSQL + R2DBC - Reactive database access
- JJWT 0.12.5 - JWT token handling
- Maven - Build tool
-
Clone the repository
git clone https://github.com/matthewhou19/auth-api-gateway.git cd auth-api-gateway -
Start PostgreSQL database
Option A: Using Docker (Recommended)
docker compose up -d
Option B: Use existing PostgreSQL
Make sure PostgreSQL is running on
localhost:5432with:- Database:
auth_gateway_db - Username:
postgres - Password:
password
- Database:
-
Run the application
cd apigateway ./mvnw spring-boot:runNote: Database tables (
users,refresh_tokens) are created automatically on startup fromschema.sql. No manual database setup required! -
Access the API
- Base URL:
http://localhost:8080 - All endpoints are prefixed with
/auth
- Base URL:
| Method | Endpoint | Description |
|---|---|---|
| POST | /auth/register |
Register a new user |
| POST | /auth/login |
Login with email/username and password |
| GET | /auth/refresh/{token} |
Refresh access token using refresh token |
| Method | Endpoint | Description |
|---|---|---|
| GET | /auth/me |
Get authenticated user details |
| DELETE | /auth/logout |
Logout and invalidate refresh token |
curl -X POST http://localhost:8080/auth/register \
-H "Content-Type: application/json" \
-d "{\"email\": \"user@example.com\", \"username\": \"johndoe\", \"name\": \"John Doe\", \"password\": \"SecurePass123@\"}"Note: Using double quotes and escaped JSON for better shell compatibility. Password uses @ instead of ! to avoid shell history expansion issues in some terminals.
curl -X POST http://localhost:8080/auth/login \
-H "Content-Type: application/json" \
-d "{\"emailUsername\": \"user@example.com\", \"password\": \"SecurePass123@\"}"Response:
{
"accessToken": "eyJhbGciOiJIUzI1NiJ9...",
"refreshToken": "base64-encoded-token"
}curl -X GET http://localhost:8080/auth/me \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"The application uses the following default database settings (matching the docker-compose.yml):
spring:
r2dbc:
url: r2dbc:postgresql://localhost:5432/auth_gateway_db
username: postgres
password: passwordFor production or custom setups, modify apigateway/src/main/resources/application.yaml:
Custom Database:
spring:
r2dbc:
url: r2dbc:postgresql://your-host:5432/your-database
username: your-username
password: your-passwordJWT Secret (CRITICAL for Production!):
jwt:
secret: your-secure-secret-key-minimum-32-characters-longThe application automatically creates the required database tables on startup:
Tables:
users- Stores user account information (id, name, username, email, password)refresh_tokens- Manages refresh token lifecycle (id, user_id, token, expiry_date, created_at)
Schema Definition:
- Table definitions are in
apigateway/src/main/resources/schema.sql - Tables are created automatically via Spring's SQL initialization feature
- Uses
CREATE TABLE IF NOT EXISTSto prevent errors on restart - Includes indexes on
refresh_tokensfor optimal performance
Configuration:
spring:
sql:
init:
mode: never # Use 'always' only in dev/test profiles; handle prod schema via migrations
platform: postgresqlNote: For production environments, use proper database migration tools like Flyway or Liquibase instead of SQL initialization mode.
- Minimum 8 characters
- At least one uppercase letter
- At least one lowercase letter
- At least one digit
- At least one special character
apigateway/
├── src/main/java/vaultweb/apigateway/
│ ├── config/ # Security & JWT configuration
│ ├── controller/ # REST API endpoints
│ ├── service/ # Business logic
│ ├── model/ # Database entities
│ ├── repositories/ # Data access layer
│ ├── dto/ # Request/Response objects
│ ├── exceptions/ # Exception handling
│ └── util/ # Utility classes
└── src/main/resources/
├── application.yaml # Application configuration
└── schema.sql # Database schema (auto-applied on startup)
[Add your license here]