A production-ready authentication and authorization service built with Spring Boot 3, Spring Security, JWT, and PostgreSQL.
This project provides secure user authentication for modern web applications using short-lived JWT access tokens and rotating refresh tokens. It is designed as the backend for the springboot-auth-web React frontend.
- User registration
- User login
- JWT access tokens
- Refresh token rotation
- Secure logout
- Stateless authentication
- Password hashing with BCrypt
- Built on Spring Security
- JWT authentication filter
- Role-based authorization
- Protected REST endpoints
- Security filter chain configuration
- CORS configuration for frontend integration
- Short-lived JWT access token
- Opaque refresh token
- Refresh token rotation
- Refresh token revocation on logout
- Secure HttpOnly cookie support
- Compatible with SPA applications
- PostgreSQL
- Spring Data JPA
- Hibernate ORM
Integrated with the companion project:
- springboot-auth-web
- React
- Vite
- TypeScript
Authentication flow:
- User logs in.
- Backend returns JWT access token.
- Backend sets refresh token cookie.
- Frontend stores access token in memory.
- Protected requests use:
Authorization: Bearer <access_token>
- When expired, frontend calls:
POST /api/auth/refresh
- Backend rotates the refresh token and returns a new access token.
| Technology | Version |
|---|---|
| Java | 21 |
| Spring Boot | 3.x |
| Spring Security | 6.x |
| Spring Data JPA | Latest |
| Hibernate | 6.x |
| PostgreSQL | 16+ |
| Maven | 3.9+ |
| JWT | JJWT |
| BCrypt | Spring Security |
src
└── main
├── java
│ └── com.richardj46.authservice
│ ├── config
│ ├── controller
│ ├── dto
│ ├── entity
│ ├── repository
│ ├── security
│ ├── service
│ └── util
└── resources
└── application.yml
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register |
Register a new user |
| POST | /api/auth/login |
Login |
| POST | /api/auth/refresh |
Refresh access token |
| POST | /api/auth/logout |
Logout |
DATABASE_URL=
DATABASE_USERNAME=
DATABASE_PASSWORD=
JWT_SECRET=
JWT_EXPIRATION=
REFRESH_TOKEN_EXPIRATION=
Example:
DATABASE_URL=jdbc:postgresql://localhost:5432/authdb
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=password
JWT_SECRET=your-256-bit-secret
JWT_EXPIRATION=900000
REFRESH_TOKEN_EXPIRATION=604800000
git clone https://github.com/<username>/springboot-auth-service.git
Create your environment variables or configure them in your IDE.
./mvnw spring-boot:run
or
mvn spring-boot:run
Register
│
▼
Login
│
▼
Access JWT + Refresh Cookie
│
▼
Protected API
│
▼
JWT Expired
│
▼
POST /api/auth/refresh
│
▼
New Access JWT
│
▼
Continue
- Stateless authentication
- JWT access tokens
- Rotating refresh tokens
- BCrypt password hashing
- Spring Security authorization
- Secure cookie support
- Refresh token revocation
- CSRF-safe authentication strategy for SPA clients
- CORS restricted to trusted frontend origins
Frontend repository:
springboot-auth-web
Built with:
- React
- Vite
- TypeScript
- Axios
- React Router
- Email verification
- Forgot password
- Password reset
- Account lockout
- Login attempt throttling
- Two-factor authentication (2FA)
- OAuth2 login (Google, GitHub)
- Role & permission management
- Admin dashboard
- Audit logging
- User profile management
MIT License