Skip to content

About

Secure role-based LMS backend with JWT authentication, Spring Security, PostgreSQL, REST APIs, Docker, and automated testing.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

LearnGrid API - Spring Boot

A backend Learning Management System built using Java and Spring Boot.

LearnGrid provides secure JWT authentication, role-based authorization, course management, student enrollment, API documentation, automated enrollment notifications, integration testing, and containerized deployment.


Features

Authentication & Authorization

  • User registration and login
  • JWT-based authentication
  • Role-based access control:
    • ADMIN
    • TEACHER
    • STUDENT
  • Password encryption using BCrypt
  • Method-level authorization using Spring Security @PreAuthorize
  • JWT authentication support in Swagger

User Management

  • View authenticated user profile
  • View all users (Admin)
  • Update user roles (Admin)
  • Secure user responses using DTOs

Course Management

  • Create courses
  • Update courses
  • Delete courses
  • View course details
  • View all courses
  • Pagination and sorting support
  • Automatic teacher assignment using the authenticated user
  • Teachers can only modify their own courses
  • Teachers can view students enrolled in their own courses

Enrollment System

  • Students can enroll in courses
  • Prevent duplicate enrollments
  • Students can view their own enrollments
  • Students can cancel enrollments
  • Teachers can view students enrolled in their courses
  • Admins can view all enrollments

Email Notifications

  • Asynchronous enrollment confirmation emails
  • Enrollment email processing using Spring's @Async
  • Gmail SMTP integration

API Features

  • Request/Response DTO pattern
  • Input validation
  • Global exception handling using @RestControllerAdvice
  • Custom exceptions
  • Appropriate HTTP status codes
  • Swagger/OpenAPI documentation
  • Pagination and sorting

Docker

  • Dockerized Spring Boot application
  • PostgreSQL running in a Docker container
  • Docker Compose for multi-container setup
  • Persistent PostgreSQL volume
  • Environment-based configuration for database credentials and JWT secret

Testing

  • 67 automated tests
  • Service-layer unit tests with Mockito
  • Repository integration tests with PostgreSQL
  • Controller integration tests using MockMvc
  • Authentication and JWT testing
  • Role-based access control testing
  • Course ownership/authorization testing
  • JWT expiration testing
  • JWT signature/tampering testing

Tech Stack

Backend

  • Java 21
  • Spring Boot 4.1
  • Spring Security
  • Spring Data JPA
  • Hibernate
  • Maven

Database

  • PostgreSQL 18

Security

  • JWT
  • BCrypt Password Encoder

Testing

  • JUnit 5
  • Mockito
  • Spring Boot Test
  • MockMvc

Documentation

  • Swagger / OpenAPI

DevOps

  • Docker
  • Docker Compose

Project Architecture

                    HTTP Request
                         |
                         v
              +---------------------+
              |   JWT Security      |
              |       Filter        |
              +---------------------+
                         |
                         v
              +---------------------+
              |     Controller      |
              +---------------------+
                         |
                         v
              +---------------------+
              |       Service       |
              +---------------------+
                         |
                         v
              +---------------------+
              |     Repository      |
              +---------------------+
                         |
                         v
                    PostgreSQL

The application follows a layered architecture:

  • Security layer validates JWT tokens and establishes the authenticated user
  • Controller layer handles HTTP requests and API responses
  • Service layer contains business logic
  • Repository layer handles database operations
  • DTOs handle API request and response objects
  • Exception layer provides centralized error handling

Database Entities

User

Stores application users.

Fields:

  • id
  • name
  • email
  • password
  • role

Roles:

  • ADMIN
  • TEACHER
  • STUDENT

Course

Stores available courses.

Fields:

  • id
  • title
  • description
  • createdAt
  • instructor

Enrollment

Connects students and courses.

Fields:

  • id
  • student
  • course
  • enrolledAt

API Documentation

Swagger UI is available at:

http://localhost:8081/swagger-ui/index.html

After logging in, JWT tokens can be supplied using the Swagger Authorize button.


API Endpoints

Authentication

Method Endpoint Access
POST /api/auth/register Public
POST /api/auth/login Public

Users

Method Endpoint Access
GET /api/users/me Authenticated
GET /api/users ADMIN
GET /api/users/admin ADMIN
PUT /api/users/{id}/role ADMIN

Courses

Method Endpoint Access
POST /api/courses ADMIN, TEACHER
GET /api/courses Authenticated
GET /api/courses/{id} Authenticated
GET /api/courses/my TEACHER
PUT /api/courses/{id} ADMIN, TEACHER
DELETE /api/courses/{id} ADMIN, TEACHER
GET /api/courses/{courseId}/students ADMIN, TEACHER

Enrollments

Method Endpoint Access
GET /api/enrollments ADMIN
POST /api/enrollments/{courseId} STUDENT
GET /api/enrollments/my STUDENT
DELETE /api/enrollments/{enrollmentId} STUDENT

Authentication

Register a user:

POST /api/auth/register

Login:

POST /api/auth/login

The login endpoint returns a JWT.

Authenticated requests use:

Authorization: Bearer <JWT>

The JWT contains the user's email and role.

Spring Security validates the token before allowing access to protected endpoints.


Running the Project

Option 1: Run Locally

1. Clone the repository

git clone https://github.com/vipul080/LearnGrid-API.git
cd LearnGrid-API

2. Configure PostgreSQL

Create a PostgreSQL database:

lms

Configure the required environment variables:

DB_URL=jdbc:postgresql://localhost:5432/lms
DB_PASSWORD=your_database_password
JWT_SECRET=your_jwt_secret

Do not commit your .env file or expose your JWT secret.

3. Build the project

Windows:

.\mvnw.cmd clean package

Linux/macOS:

./mvnw clean package

4. Run the application

.\mvnw.cmd spring-boot:run

The application runs on:

http://localhost:8081

Swagger UI:

http://localhost:8081/swagger-ui/index.html

Running with Docker

Make sure Docker Desktop is installed and running.

1. Configure environment variables

Create a .env file in the project root:

DB_PASSWORD=your_database_password
JWT_SECRET=your_jwt_secret

The .env file should not be committed to GitHub.

2. Build and start the application

docker compose up --build

This starts:

Spring Boot application
        |
        v
PostgreSQL container

The application will be available at:

http://localhost:8081

Swagger UI:

http://localhost:8081/swagger-ui/index.html

3. Stop the application

docker compose down

PostgreSQL data is stored in a persistent Docker volume, so normal container shutdowns do not remove the database data.


Running Tests

Run the complete test suite using Maven.

Windows

.\mvnw.cmd test

Linux/macOS

./mvnw test

Current test result:

67 tests
0 failures
0 errors
0 skipped

The test suite includes:

  • 14 service-layer unit tests
  • Repository integration tests
  • Controller integration tests
  • Authentication integration tests
  • JWT security tests
  • Role-based authorization tests
  • Ownership/authorization tests

Example Workflow

Teacher

  1. Register as a teacher
  2. Login and receive a JWT token
  3. Create a course
  4. Manage their own courses
  5. View students enrolled in their courses

Student

  1. Register as a student
  2. Login and receive a JWT token
  3. Browse available courses
  4. Enroll in a course
  5. Receive an enrollment confirmation email
  6. View enrolled courses
  7. Cancel an enrollment

Admin

  1. Login and receive a JWT token
  2. View registered users
  3. Manage user roles
  4. Manage courses
  5. View all enrollments

Future Improvements

  • Frontend application
  • Course categories
  • File uploads for course materials
  • Cloud deployment
  • Additional API features

Author

Vipul Shukla

About

Secure role-based LMS backend with JWT authentication, Spring Security, PostgreSQL, REST APIs, Docker, and automated testing.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages