A Spring Boot microservice providing JWT-based authentication with MySQL database backend.
- JWT token-based authentication
- Refresh token support
- User authentication with BCrypt password hashing
- Comprehensive audit logging
- Docker support
- OpenAPI/Swagger documentation
- MySQL database with Flyway migrations
- Java 17+
- Maven 3.6+
- MySQL 8.0+ (or Docker)
- Docker & Docker Compose (optional)
- Clone the repository
- Run the application:
docker compose up -dThe service will be available at http://localhost:8080
- Set up MySQL database:
CREATE DATABASE auth_db;
CREATE USER 'auth_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON auth_db.* TO 'auth_user'@'localhost';- Configure environment variables:
export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_DATABASE=auth_db
export MYSQL_USERNAME=auth_user
export MYSQL_PASSWORD=secure_password
export JWT_SECRET=your-secret-key-here- Build and run:
mvn clean package
java -jar target/auth-*.jar- POST
/api/auth/register - Request:
{
"username": "newuser",
"email": "newuser@example.com",
"password": "password123"
}- Response:
{
"accessToken": "eyJhbGciOiJIUzUxMi...",
"refreshToken": "550e8400-e29b-41d4-a716-446655440000",
"tokenType": "Bearer",
"expiresIn": 3600
}- POST
/api/auth/login - Request:
{
"username": "testuser",
"password": "password123"
}- Response:
{
"accessToken": "eyJhbGciOiJIUzUxMi...",
"refreshToken": "550e8400-e29b-41d4-a716-446655440000",
"tokenType": "Bearer",
"expiresIn": 3600
}- POST
/api/auth/validate - Request:
{
"token": "eyJhbGciOiJIUzUxMi..."
}- Response:
{
"valid": true,
"username": "testuser",
"expiresAt": "2024-01-01T12:00:00"
}- POST
/api/auth/refresh - Request:
{
"refreshToken": "550e8400-e29b-41d4-a716-446655440000"
}- Response:
{
"accessToken": "eyJhbGciOiJIUzUxMi...",
"refreshToken": "660e8400-e29b-41d4-a716-446655440000",
"tokenType": "Bearer",
"expiresIn": 3600
}- GET
/api/auth/user - Headers:
Authorization: Bearer <accessToken> - Response:
{
"id": 1,
"username": "testuser",
"email": "testuser@example.com",
"enabled": true
}The following test users are created by default:
demo- Demo account (password:password)admin- Administrator account (password:password123)testuser- Regular user (password:password123)johndoe- Regular user (password:password123)janedoe- Regular user (password:password123)disableduser- Disabled account (password:password123)
Swagger UI is available at: http://localhost:8080/swagger-ui.html
Key configuration properties in application.yml:
jwt:
secret: ${JWT_SECRET:your-secret-key}
expiration: ${JWT_EXPIRATION:3600000} # 1 hour
refresh-token-expiration: ${REFRESH_TOKEN_EXPIRATION:604800000} # 7 daysThe service uses three main tables:
users- User accountsrefresh_tokens- Refresh token storageaudit_logs- Authentication audit trail
Health check endpoint: GET /actuator/health
- Change the default JWT secret in production
- Use HTTPS in production
- Configure CORS appropriately for your frontend
- Review and adjust token expiration times
- Implement rate limiting for login attempts
- Regular security audits of dependencies
This project includes a Makefile for common development tasks:
make help # Show all available commandsmake setup # Initial project setup (install deps, start services)
make build # Build the application JAR
make test # Run all tests
make test-coverage # Run tests with coverage report
make run # Run application locallymake up # Start MySQL and application containers
make down # Stop all containers
make logs # Show container logs
make logs-app # Show application logs only
make status # Show service statusmake docker-build # Build Docker image with version tag
make docker-build-prod # Build production-optimized image
make docker-run # Run container (requires external MySQL)
make docker-clean # Remove application Docker imagesmake db-migrate # Run database migrations
make db-info # Show migration status
make db-reset # Reset database (WARNING: destroys data)make lint # Check code formatting
make format # Format code
make deps # Download dependencies
make deps-check # Check for dependency updatesmake test-api # Test API endpoints
make health # Check application health
make api-docs # Instructions to open API documentationmake client # Run auth client
make client-docker-build # Build client Docker image
make client-docker-run # Run client in Dockermake kube # Deploy to Kubernetes (Deployment)
make kube-clean # Remove from Kubernetes
make kube-statefulset # Deploy as StatefulSet (stable pod names, persistent storage)
make kube-statefulset-clean # Remove StatefulSet deploymentmake env-example # Create .env.example fileNote: For version management, use the root Makefile:
cd ../.. && make version # Show current version
cd ../.. && make bump-version # Bump to next patch versionIf you prefer using Maven directly:
./mvnw clean package./mvnw test./mvnw clean test jacoco:reportThis project is licensed under the MIT License.