A robust Spring Boot authentication system with JWT, Email Verification, and Role-Based Access Control.
Create a MySQL database named springdb:
CREATE DATABASE springdb;Create a .env file in the root directory or set these in your IDE/System:
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-app-password(Note: Use a Gmail App Password, not your regular password)
./mvnw spring-boot:runServer starts at: http://localhost:8080
src/main/java/com/arpon007/FullStackAuth
โโโ config/ # Security & App Config (SecurityConfig, Cors)
โโโ Controller/ # API Endpoints (AuthController, ProfileController)
โโโ Service/ # Business Logic (EmailService, ProfileService)
โโโ Entity/ # Database Models (UserEntity, RoleEntity)
โโโ repository/ # Database Access (UserRepository)
โโโ Filter/ # JWT Request Filter
โโโ Util/ # Utilities (JwtUtil)
โโโ Io/ # DTOs (Request/Response objects)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Spring Boot Application โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ HTTP Requests (REST API) โ โ
โ โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ AuthController โ โ ProfileController โ โ
โ โ โโ POST /auth/login โ โ โโ POST /auth/registerโ โ
โ โ โโ GET /auth/isAuthenticatedโ โ โโ GET /profile โ โ
โ โ โโ GET /auth/verify โ โ โโ PUT /profile โ โ
โ โ โโ POST /auth/request- โ โ โ โ
โ โ โ password-reset โ โโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โโ POST /auth/reset-passwordโ โ
โ โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Security Filter Chain (JwtRequestFilter) โ โ
โ โ 1. Extract JWT from Authorization header or cookie โ โ
โ โ 2. Validate token signature โ โ
โ โ 3. Check token expiration โ โ
โ โ 4. Extract user email from token claims โ โ
โ โ 5. Set SecurityContext with authentication โ โ
โ โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Service Layer (Business Logic) โ โ
โ โ โโ AppUserDetailsService (Load user credentials) โ โ
โ โ โโ ProfileService (User profile operations) โ โ
โ โ โโ EmailService (Send verification/reset emails) โ โ
โ โ โโ JwtUtil (Generate & validate JWT tokens) โ โ
โ โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Repository Layer (Data Access) โ โ
โ โ โโ UserRepository (Database queries) โ โ
โ โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Database (MySQL) โ โ
โ โ โโ UserEntity (User data with security fields) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Client Server โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ 1. Submit login form โ
โ POST /api/v1/auth/login โ
โ { โ
โ "email": "user@example.com", โ
โ "password": "secret123" โ
โ } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโบ โ
โ โ
โ โโ Verify credentials โ
โ โ (AuthenticationManager)
โ โ โ
โ โโ Check if account โ
โ โ is verified โ
โ โ (MUST be verified!) โ
โ โ โ
โ โโ Load user details โ
โ โ (AppUserDetailsService)
โ โ โ
โ โโ Generate JWT token โ
โ โ (JwtUtil) โ
โ โ โ
โ โโ Create HTTP-only โ
โ โ cookie โ
โ โ โ
โ 2. Receive token & cookie (OR ERROR if unverified) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ Success: โ
โ { โ
โ "email": "user@example.com", โ
โ "token": "eyJhbGc..." โ
โ } โ
โ Set-Cookie: jwt=eyJhbGc...; HttpOnly; Path=/ โ
โ โ
โ 3. Store token & cookie (if successful) โ
โ - Token: localStorage or sessionStorage (for manual sending) โ
โ - Cookie: Automatically managed by browser โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. User registers (POST /api/v1/auth/register) โ
โ { name, email, password } โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. User account created โ
โ isAccountVerified = false โ
โ Verification token (UUID) generated automatically โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Verification email sent automatically โ
โ EmailService.sendVerificationLinkEmail() โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. User clicks verification link โ
โ GET /api/v1/auth/verify?token=abc-123-def... โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 5. Mark account as verified โ
โ isAccountVerified = true โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Refresh Tokens: For long-lived sessions without re-login.
- OAuth2 Login: Sign in with Google.
- Two-Factor Auth (2FA): Extra security layer.
- Rate Limiting: Prevent abuse of APIs.
- Audit Logs: Track important security events.
- Create Google Cloud Project: Go to Google Cloud Console.
- Enable APIs: Enable "Google People API" or just "Google+ API" (legacy) - actually just "Google Identity" setup.
- Create Credentials:
- Create OAuth Client ID.
- Application Type: Web Application.
- Authorized Redirect URIs:
http://localhost:8080/api/v1/login/oauth2/code/google
- Update .env:
GOOGLE_CLIENT_ID=your-client-id GOOGLE_CLIENT_SECRET=your-client-secret
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/v1/oauth2/authorization/google |
Initiate Google Login (Browser) |
GET |
/api/v1/login/oauth2/code/google |
Callback URL (Handled by Spring Security) |
Step 1: Locate the file Auth rest apis.postman_collection.json in the project root.
Step 2: Import this file into Postman.
Step 3: Start testing the endpoints!
All endpoints are prefixed with /api/v1
| Method | Endpoint | Description |
|---|---|---|
POST |
/auth/login |
Login & get JWT token |
POST |
/auth/register |
Signup & receive verification email |
GET |
/auth/verify |
Verify email (via link/token) |
POST |
/auth/resend-verification |
Resend verification email |
POST |
/auth/request-password-reset |
Request password reset link |
POST |
/auth/reset-password |
Reset password with token |
Requires Authorization: Bearer <token> header
| Method | Endpoint | Description |
|---|---|---|
GET |
/profile/me |
Get current user details |
PUT |
/profile/me |
Update profile |
POST |
/profile/change-password/init |
Initiate password change (sends OTP) |
POST |
/profile/change-password/verify |
Verify OTP & update password |
POST |
/profile/change-email/init |
Initiate email change (sends OTP to new email) |
POST |
/profile/change-email/verify |
Verify OTP & update email |
New users are USER by default. To make an admin:
- Register a user.
- Run SQL:
INSERT INTO tbl_user_roles (user_id, role_id) VALUES (1, 2);(Adjust IDs as needed).