Production-ready authentication starter kit for Spring Boot
| Feature | Status |
|---|---|
| JWT Authentication (Access + Refresh Tokens) | β |
| BCrypt Password Hashing | β |
| Role-Based Access Control (USER, ADMIN) | β |
| Token Refresh with Rotation | β |
| Password Recovery (reset token) | β |
| CORS Configuration | β |
| Global Exception Handling | β |
| Frontend Demo (Login, Register, Dashboard, Admin) | β |
| Docker Compose (PostgreSQL + Backend + Frontend) | β |
| OAuth2 (Google, GitHub) | β |
| Two-Factor Authentication (TOTP) | β |
| Rate Limiting (Bucket4j) | β |
| Feature Flags (Toggle features via env vars) | β |
| Email Service (Verification + Password Reset) | β |
| Unit Tests + JaCoCo Coverage (100%) | β |
| SonarQube Code Quality (0 Bugs/Smells) | β |
git clone https://github.com/FirstOnDie/authforge.git
cd authforge
docker-compose up --buildOpen http://localhost:4000 β Ready! π
If you modify the source code, you can apply the changes by rebuilding the containers:
docker-compose down
docker-compose up --build -d| Service | Port | URL |
|---|---|---|
| Frontend (Nginx) | 4000 | http://localhost:4000 |
| Backend (Spring Boot) | 8090 | http://localhost:8090 |
| PostgreSQL | 5433 | β |
| MailHog (Email UI) | 8025 | http://localhost:8025 |
| SonarQube (separate) | 9000 | http://localhost:9000 |
Toggle features on/off via environment variables β no code changes needed.
| Flag | Env Variable | Default |
|---|---|---|
| OAuth2 Login | FEATURE_OAUTH2 |
true |
| Two-Factor Auth | FEATURE_2FA |
true |
| Rate Limiting | FEATURE_RATE_LIMIT |
true |
| Email Verification | FEATURE_EMAIL |
true |
Active flags are exposed at GET /api/admin/features (admin only).
Uses MailHog in Docker for local email testing (no real emails sent).
- Verification emails: sent on registration when
FEATURE_EMAIL=true - Password reset emails: HTML emails with reset links
- MailHog UI: http://localhost:8025 to view all captured emails
AuthForge ensures high code quality and reliability.
cd backend
mvn clean testJaCoCo coverage report: backend/target/site/jacoco/index.html
Runs in a separate Docker Compose file to keep the main stack lightweight. It enforces a Strict Quality Gate (0 Bugs, 0 Vulnerabilities, 0 Code Smells, 100% Coverage).
# Start SonarQube
docker-compose -f docker-compose.sonar.yml up -d
# Wait ~1 min for startup, then run analysis
cd backend
mvn clean verify sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=adminDefault credentials: admin / admin (change on first login).
AuthForge uses Swagger UI (OpenAPI 3) for interactive API documentation. Once the application is running, you can explore and test all endpoints directly from your browser:
π http://localhost:8090/swagger-ui.html
(To use protected endpoints in Swagger, click the Authorize button and input your Bearer <accessToken>)
Below is a brief summary of the available endpoints.
Endpoints for login, registration, and managing access tokens.
Creates a new account.
- Access: Public
- Request Body:
{ "name": "John Doe", "email": "john@example.com", "password": "SecurePassword123" }
Authenticates a user and issues JWT tokens.
- Access: Public
- Request Body:
{ "email": "john@example.com", "password": "SecurePassword123" } - Response: Returns an
accessTokenand arefreshToken. If 2FA is enabled, it returnsrequiresTwoFactor: trueand no tokens.
Exchanges a valid Refresh Token for a new Access Token.
- Access: Public
- Request Body:
{ "refreshToken": "your-refresh-token-here" }
Invalidates the current Refresh Token.
- Access: Authenticated (Requires Bearer Token)
- Request Body:
{ "refreshToken": "your-refresh-token-here" }
Initates the password reset flow by email.
- Access: Public
- Request Body:
{ "email": "john@example.com" }
Resets the password using the token sent to the email.
- Access: Public
- Request Body:
{ "token": "reset-token-received-via-email", "newPassword": "NewSecurePassword123" }
Endpoints for setting up and verifying TOTP codes.
Generates a TOTP secret and QR code URI for Authenticator apps.
- Access: Authenticated (Requires Bearer Token)
- Response: Returns
secretKeyandqrCodeUrl.
Verifies a TOTP code and activates 2FA on the account.
- Access: Authenticated (Requires Bearer Token)
- Request Body:
{ "code": "123456" }
Disables Two-Factor Authentication.
- Access: Authenticated (Requires Bearer Token)
Step 2 of login when 2FA is enabled. Validates code and issues JWT tokens.
- Access: Public
- Request Body:
{ "email": "john@example.com", "code": "123456" }
Endpoints related to the logged-in user.
Fetches the current user's profile information.
- Access: Authenticated (Requires Bearer Token)
- Response Example:
{ "id": 1, "name": "John Doe", "email": "john@example.com", "role": "USER", "emailVerified": true, "twoFactorEnabled": false, "oauth2Provider": "local" }
Administrative actions, available only to accounts with the ADMIN role.
Fetches a list of all registered users.
- Access:
ADMINOnly
Changes the role of a user.
- Access:
ADMINOnly - Path Parameter:
id- The numeric ID of the user. - Request Example:
?role=ADMIN(as Request Parameter)
Fetches the active states of system feature flags.
- Access:
ADMINOnly
βββββββββββββββββββββββββββββββββββββββββββββββ
β Docker Compose β
ββββββββββββ¬βββββββββββββββ¬ββββββββββββββββββββ€
β Frontend β Backend β PostgreSQL β
β Nginx β Spring Boot β β
β :4000 β :8090 β :5433 β
β β β β
β HTML/CSS β Controllers β users table β
β JS β Services β refresh_tokens β
β β Security β β
β /api/* βββΊ JWT Filter β β
β β BCrypt β β
ββββββββββββ΄βββββββββββββββ΄ββββββββββββββββββββ
All configuration is done via environment variables (see .env.example):
| Variable | Default | Description |
|---|---|---|
DB_URL |
jdbc:postgresql://postgres:5432/authforge |
Database URL |
DB_USERNAME |
authforge |
Database user |
DB_PASSWORD |
authforge |
Database password |
JWT_SECRET |
(change me!) | HMAC-SHA256 signing key |
CORS_ORIGINS |
http://localhost:4000 |
Allowed CORS origins |
GOOGLE_CLIENT_ID |
β | Google OAuth2 Client ID |
GOOGLE_CLIENT_SECRET |
β | Google OAuth2 Client Secret |
GITHUB_CLIENT_ID |
β | GitHub OAuth2 Client ID |
GITHUB_CLIENT_SECRET |
β | GitHub OAuth2 Client Secret |
OAUTH2_REDIRECT_URI |
http://localhost:4000 |
Frontend redirect after OAuth2 |
- Go to Google Cloud Console β APIs & Services β Credentials
- Create an OAuth 2.0 Client ID (Web Application)
- Set Authorized redirect URI:
http://localhost:8090/login/oauth2/code/google - Copy the Client ID and Client Secret into your
.envfile
- Go to GitHub Settings β OAuth Apps β New OAuth App
- Set Authorization callback URL:
http://localhost:8090/login/oauth2/code/github - Copy the Client ID and Client Secret into your
.envfile
Auth endpoints (/api/auth/**) are rate-limited to prevent brute-force attacks.
| Setting | Default | Env Variable |
|---|---|---|
| Requests per minute (per IP) | 30 | RATE_LIMIT_RPM |
When the limit is exceeded, the API returns HTTP 429 Too Many Requests.
- v1.0 β JWT Auth, Roles, Password Recovery, Docker
- v1.1 β OAuth2 (Google, GitHub)
- v1.2 β 2FA (TOTP), Rate Limiting
- v2.0 β Email Service, Account Verification, Feature Flags, 100% Coverage, 0 SonarQube issues
MIT β Use this starter kit freely in your projects.
Built with β Java 21 + π Spring Boot 3 + π‘οΈ Spring Security 6
by Carlos ExpΓ³sito