Production-ready REST API built with Spring Boot implementing a complete JWT-based authentication and authorization system, including:
- ✅ JWT Login
- ✅ Access Token & Refresh Token
- ✅ Role-based Authorization
- ✅ Docker + MySQL
- ✅ Global Exception Handling (
@ControllerAdvice) - ✅
SecurityFilterChainConfiguration - ✅ Token Refresh Flow
- ✅ Clean
User→UserDetailsMapping
- Java 17+
- Spring Boot 3+
- Spring Security
- JWT (JJWT)
- MySQL
- Docker & Docker Compose
- Lombok
- gradle
| Token | Lifetime | Purpose |
|---|---|---|
| Access Token | 10–15 minutes | Access protected endpoints |
| Refresh Token | 7–30 days | Generate new access tokens |
POST /api/auth/login
Response:
{
"accessToken": "...",
"refreshToken": "..."
}POST /api/auth/refresh
Request Body:
{
"refreshToken": "..."
}Response:
{
"accessToken": "new-access-token"
}Service responsible for:
- Generating Access Tokens
- Generating Refresh Tokens
- Extracting username from token
- Validating token signature and expiration
Utility class that converts the User entity into a UserDetails instance:
public class UserDetailsFactory {
public static UserDetails create(User user) {
List<GrantedAuthority> authorities = user.getRoles().stream()
.map(role -> new SimpleGrantedAuthority(role.getName()))
.toList();
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
user.isEnabled(),
true,
true,
true,
authorities
);
}
}- User logs in
- Receives Access Token + Refresh Token
- Access Token expires
- Frontend sends Refresh Token
- Backend validates Refresh Token
- New Access Token is generated ✅
- Roles are mapped to
GrantedAuthority - Roles are included as
claimsinside JWT - Access rules defined using:
.hasAuthority("ADMIN")The project uses:
- MySQL container
- Backend connected via
application.properties
Implemented using @ControllerAdvice for:
- Validation errors
- Authentication errors
- Resource not found
- Custom business exceptions
- Layered architecture
- DTOs for requests and responses
- Stateless security
- Decoupled role management
- Token expiration control
- Entity to
UserDetailsmapping
✔️ Production-ready ✔️ Complete security layer ✔️ Clean architecture ✔️ Ready for deployment
Developed as part of an advanced backend learning process using Spring Boot.
🔥 This project follows real-world backend security standards and practices.