LetsPlay is a Java Spring Boot REST API project implementing user and product management with authentication and authorization powered by JWT. It uses MongoDB for data persistence. The project supports typical operations such as user registration, login, profile management, and CRUD operations for products. It employs role-based access control with roles "USER" and "ADMIN" for secure endpoint access.
- User Authentication: JWT-based login and role assignment.
- Role-Based Authorization: Protect endpoints by roles USER or ADMIN.
- User Management: Admin can create, update, promote, and delete users.
- Product Management: Authenticated users can create, update, delete their products; public can view products.
- Validation and Exception Handling: Input validation and centralized error handling.
- Security: Stateless sessions, JWT filter, strong password encoding.
- CORS Support: Configured for secure, restricted front-end access.
POST /api/auth/login— User login and JWT token retrievalPOST /api/auth/register— User self-registration (if implemented)
GET /api/users— ADMIN only: list all usersGET /api/users/{id}— Get user by ID (ADMIN or self depending on config)GET /api/users/me— Authenticated user’s own profilePOST /api/users/create— ADMIN only: create new userPUT /api/users/update/{id}— Authenticated USER/ADMIN: update user profile or admin edits any userPUT /api/users/promote/{id}— ADMIN only: promote user rolesDELETE /api/users/{id}— ADMIN only: delete user
GET /api/products— Public: list all productsGET /api/products/{id}— Public: get product by IDGET /api/users/{userId}/products— Public: get all products for a userPOST /api/products— Authenticated USER/ADMIN: create productPUT /api/products/{id}— Authenticated USER/ADMIN: update product (own or admin)DELETE /api/products/{id}— Authenticated USER/ADMIN: delete product (own or admin)
- Java 17+
- Spring Boot 3.x
- Spring Security with JWT
- MongoDB and Spring Data MongoDB
- Jakarta Validation API (
@Valid,@NotBlank, etc.) - Maven or Gradle build tool
-
Prerequisites:
- Java 17+ installed
- MongoDB server running locally or remotely
-
Clone the repository:
git clone https://github.com/01-edu/public.git cd public/subjects/java/projects/lets-play -
Configure MongoDB
Adjust MongoDB connection settings inapplication.propertiesorapplication.ymlfile. -
Build and Run
./mvnw clean install ./mvnw spring-boot:run -
Testing
Run tests:./mvnw test -
API Usage
- Use Postman
- Register/login to obtain JWT token.
- Include
Authorization: Bearer <token>header for protected operations.
- Stateless JWT filter applied before username/password filter.
- Role-based endpoint protection via
hasAnyRolerules. - Public access to authentication and product browsing endpoints.
- CORS restricted to
https://localhost:8443. - Passwords hashed with BCrypt encoder.
authpackage: JWT utilities and filters.controllerpackage: RestControllers with endpoint mappings and role checks.modelpackage: MongoDB entities with validation.servicepackage: Business logic, validation, and repository interactions.repositorypackage: Spring Data MongoDB repositories.requestpackage defines Data Transfer Objects (DTOs) for incoming API request payloads with validation.responsepackage defines DTOs shaping the API response objects.exceptionpackage: Custom exceptions and a global exception handler returning standardized JSON error responses.configpackage contains Spring Boot configuration classes including security and CORS setup.
- Carefully maintain synchronization between your SecurityConfig roles and JWT authorities.
- Maintain strong validation in service methods to prevent 500 errors.
- Customize exceptions via
GlobalExceptionHandlerfor consistent API error responses.