This project is a Spring Boot REST API that implements JWT-based authentication and authorization with User, Category, and Product management.
It follows clean architecture using:
- Controller layer
- Service layer
- Repository layer
- DTOs & Mappers
- Spring Security with JWT
- User Registration & Login
- JWT Token Generation & Validation
- Role-based Authentication
- Secure APIs using Spring Security
- Category & Product CRUD APIs
- Stateless Authentication (No Sessions)
src/main/java/com/categories/product
│
├── controller/
│ ├── UserController
│ ├── CategoryController
│ └── ProductController
│
├── dto/
│ ├── UserRequestDTO
│ ├── UserResponseDTO
│ ├── CategoryRequest
│ ├── CategoryResponse
│ ├── ProductRequest
│ ├── ProductResponse
│ └── ExceptionResponseDTO
│
├── entities/
│ ├── User
│ ├── Role
│ ├── Category
│ └── Product
│
├── mapper/
│ ├── UserMapper
│ ├── CategoryMapper
│ └── ProductMapper
│
├── repositories/
│ ├── UserRepository
│ ├── CategoryRepository
│ └── ProductRepository
│
├── serviceImpl/
│ ├── CustomUserDetailsService
│ ├── CategoryServiceImpl
│ └── ProductServiceImpl
│
├── security/
│ ├── JwtUtil
│ ├── JwtRequestFilter
│ ├── SecurityConfig
│ ├── UserPrincipal
│ ├── JwtAccessDeniedHandler
│ └── JwtAuthenticationEntryPoint
| Component | Responsibility |
|---|---|
JwtUtil |
Generate & validate JWT tokens |
JwtRequestFilter |
Intercepts every request & validates JWT |
SecurityConfig |
Configures Spring Security rules |
UserPrincipal |
Adapts User entity for Spring Security |
CustomUserDetailsService |
Loads user from DB for authentication |
Client → UserController → AuthenticationManager
→ CustomUserDetailsService → UserPrincipal
→ JwtUtil → JWT Token → Client
Client (JWT)
↓
JwtRequestFilter
↓
JwtUtil validates token
↓
SecurityContextHolder populated
↓
Controller executes
Spring Security does not know how to read users from your database.
So:
- You implement
UserDetailsService - Override
loadUserByUsername() - Fetch user from DB
- Convert it into
UserPrincipal
return new UserPrincipal(user.get());👉 This makes your User understandable to Spring Security.
-
Wraps
Userentity -
Implements
UserDetails -
Provides:
- Username
- Password
- Roles (Authorities)
📌 Think of it as a passport for Spring Security.
Optional<User> user = userRepository.findByUsername(username);
return new UserPrincipal(user.get());OptionalavoidsNullPointerExceptionuser.get()extracts actualUserobject- That user is wrapped inside
UserPrincipal
/user/register → Public
/user/login → Public
/api/** (GET) → Public (Optional)
Others → JWT Required
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...- Java 17+
- Spring Boot
- Spring Security
- JWT (jjwt)
- Spring Data JPA
- MySQL / PostgreSQL
- Maven
SecurityConfig→ Security rules & filtersJwtRequestFilter→ Runs on every requestJwtUtil→ Token generator & validatorUserPrincipal→ Bridge between DB User & Spring SecurityCustomUserDetailsService→ Loads user from DB
Abhishek Brahmbhatt Spring Boot & Backend Developer 🚀
This project follows industry-standard JWT authentication architecture and is ideal for:
- Learning Spring Security
- Backend interviews
- Real-world REST APIs
Feel free to extend it with refresh tokens, OAuth, or RBAC.