Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mohamad-shosha/spring-jpa-mvc-security

This project integrates Spring MVC, JPA, and Hibernate for managing Instructor, Address, and Course entities. It uses Liquibase for database management and Postman for API testing. Key features include creating, reading, updating, and deleting entities, along with security to ensure authentication, authorization measures for safe data handling.
https://github.com/mohamad-shosha/spring-jpa-mvc-security

bootstrap css database error-handling hibernate html5 java jpa-entities liquibase maven mysql spring-boot spring-data-jpa spring-mvc spring-security thymeleaf

Last synced: 6 days ago
JSON representation

This project integrates Spring MVC, JPA, and Hibernate for managing Instructor, Address, and Course entities. It uses Liquibase for database management and Postman for API testing. Key features include creating, reading, updating, and deleting entities, along with security to ensure authentication, authorization measures for safe data handling.

Awesome Lists containing this project

README

        

# ๐ŸŒŸ Spring-MVC-JPA-Security

This project showcases the powerful integration of **Spring MVC** with **Java Persistence API (JPA)**, utilizing **Hibernate** for efficient Object-Relational Mapping (ORM).

### ๐ŸŒŸ Key Features:
- **Seamless Backend Integration**: Effortlessly manage and query data with a robust backend framework.

- **Core Entities**:
- **Instructor**: Manage instructor details and associated courses.
- **Address**: Handle address information for instructors and courses.
- **Course**: Organize and maintain course offerings and their related entities.

- **Database Schema Management**: Leverage **Liquibase** to manage database versioning and migrations, ensuring smooth transitions during updates.

- **API Testing**: Utilize **Postman** as a client for testing API endpoints, making it easy to verify and validate the functionality of your application.

With this setup, you can efficiently build and maintain a robust application tailored to your data management needs!

## ๐Ÿ“ Table of Contents
- [Features](#features)
- [Prerequisites](#prerequisites)
- [Getting Started](#getting-started)
- [Security](#security)
- [Technologies](#technologies)
- [Demo](#demo)
- [Docker](#docker)
- [Resources](#resources)

## ๐Ÿš€ Features

This project boasts a range of powerful features that streamline your application development and enhance user experience:

### ๐ŸŒŸ Key Functionalities

- **CRUD Operations**: Effortlessly perform *Create, Read, Update, and Delete* operations for Instructor, Address, and Course entities.

- **MVC Architecture**: Utilize a clean and organized *Model-View-Controller* architecture for clear separation of concerns.

- **Spring Boot Integration**: Benefit from *Spring Boot* for rapid backend development with automatic configuration and dependency management.

- **Efficient ORM**: Leverage *JPA & Hibernate* for smooth Object-Relational Mapping and database interactions.

- **Dynamic Web Pages**: Render interactive and dynamic web pages with *Thymeleaf*.

- **Database Management**: Manage database schema versioning and migrations seamlessly with *Liquibase*.

- **API Testing Made Easy**: Use *Postman* for quick and efficient testing of your API endpoints.

---

### ๐ŸŒˆ Explore the Features

#### 1. **Create**
- Easily add new Instructor, Address, or Course entities through a user-friendly web form.

#### 2. **Read**
- Retrieve and display data for Instructor, Address, or Course entities in a well-structured format.

#### 3. **Update**
- Modify existing entities with an intuitive interface for effortless changes.

#### 4. **Delete**
- Remove entities with a single click, simplifying data management.

---

With these features, your application will be robust, user-friendly, and maintainable!

Remove entities with a single click.

---

## ๐Ÿ“– Prerequisites

Before you begin, ensure you have the following:

- *Java Development Kit (JDK)* installed (preferably JDK 11 or later).
- *Maven* installed for dependency management.
- Your favorite IDE (e.g., IntelliJ IDEA, Eclipse) for code editing.
- *Postman* for API testing.

---

## โœจ Getting Started

Follow these steps to set up your Spring Boot project quickly and efficiently:

### ๐Ÿš€ Step 1: Create a Spring Boot Project

Begin by generating a new Spring Boot project using [Spring Initializr](https://start.spring.io/) with the following settings:

- **Project**: Maven Project
- **Language**: Java
- **Spring Boot**: Latest stable version
- **Packaging**: Jar
- **Dependencies**:
- Spring Web
- Spring Data JPA
- Thymeleaf
- Liquibase
- MySQL Driver

Click **"Generate"** to download the project zip file.

---

### ๐Ÿ“ฅ Step 2: Extract and Import into IDE

1. Extract the downloaded zip file.
2. Import the project into your preferred IDE (e.g., IntelliJ IDEA, Eclipse).

---

### โš™๏ธ Step 3: Configure Database and Liquibase

- **Database Configuration**: Update your `application.properties` or `application.yml` file with your database connection details.

- **Liquibase Configuration**: Configure Liquibase in your `application.properties` and add your changelog files in `src/main/resources/db/changelog`.
```application.properties
spring.liquibase.change-log=db/changelog/db_master_changelog.xml
```

---

### ๐Ÿ—๏ธ Step 4: Define Entities

Create entity classes for **Instructor**, **Address**, and **Course** in your project.

---

### ๐Ÿ“š Step 5: Create Repository Interfaces

Define repository interfaces for each entity to enable CRUD operations.

---

### ๐Ÿ”„ Step 6: Implement Service Layer

Develop a service layer to manage business logic and facilitate interaction with repositories.

---

### ๐Ÿ–ฅ๏ธ Step 7: Implement Controllers

Create MVC controllers to manage HTTP requests and return appropriate views.

---

### ๐ŸŽจ Step 8: Create Thymeleaf Views

Design Thymeleaf templates for displaying forms and results.

---

### ๐Ÿงช Step 9: Test Using Postman

Utilize Postman to test your API endpoints by sending requests to create, read, update, and delete entities.

---

### โ–ถ๏ธ Step 10: Run Your Application

Run your Spring Boot application. The server will start at [http://localhost:8082](http://localhost:8082).

---

## ๐Ÿ“š Technologies

- **Spring Boot**: For building the backend application.
- **Spring MVC**: For handling web requests and rendering views.
- **JPA & Hibernate**: For ORM and database interactions.
- **Thymeleaf**: For server-side templating.
- **Liquibase**: For database schema management and migrations.
- **MySQL**: As the database.
- **Postman**: For API testing.

---

## ๐Ÿ”’ Security

Ensuring the security of your application is crucial. Here are some recommendations and practices to secure your Spring MVC application:

### 1. **Use Spring Security**

Integrate Spring Security to handle authentication and authorization. This framework provides comprehensive security features, including:

- **Authentication**: Secure your endpoints by requiring users to log in.
- **Authorization**: Control access to various resources based on user roles.

**Setup Example**:
Add Spring Security dependency in your `pom.xml`:
```xml

org.springframework.boot
spring-boot-starter-security

```
### 2. **Secure Sensitive Information**

- **Environment Variables**: Store sensitive data (like database passwords) in environment variables instead of hardcoding them in your `application.properties` file. For example:
```application.properties
database.url=jdbc:mysql://localhost:3307/mydb
database.username=root
database.password=shosh404@@

spring.datasource.url=${database.url}
spring.datasource.username=${database.username}
spring.datasource.password=${database.password}
```

Step 1: Add Spring Security Dependency.๐Ÿ›ก๏ธ
In your pom.xml (for Maven) or build.gradle (for Gradle), add the Spring Security dependency

Step 2: Handle User Authentication.๐Ÿ”
This App uses in-memory authentication ,this step provide username,password and role For a production application.

Step 3: Secure Endpoints
By specify which endpoints require authentication and authorization by configuring HttpSecurity.

Step 4: Define User Roles.๐Ÿ—๏ธ
Define roles that represent different levels of access in your application , For example that apply on my app, you might have roles like ROLE_EMPLOYEE , ROLE_MANAGER and ROLE_ADMIN.
You can assign roles to users during user authentication.

## ๐ŸŽฅ Demo

Get a firsthand look at the project in action! See how the features come together and experience the functionality of the application.

๐Ÿ‘‰ [**Watch the Demo Video Here**](https://drive.google.com/drive/folders/1Z7WG3Q52P4SGVnE3FR706UXGT05RHmjF)

---

## ๐Ÿณ Docker

To push your Docker image to Docker Hub, use the following command:

```bash
docker push your-docker-repo/spring-mvc-app:latest
```

## ๐Ÿ—‚ Resources

- [Spring Boot Documentation](https://spring.io/projects/spring-boot) - Official documentation for Spring Boot.
- [Spring MVC Documentation](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc) - Comprehensive guide to Spring MVC framework.
- [JPA and Hibernate Guide](https://docs.jboss.org/hibernate/orm/current/userguide/html_single/) - Detailed documentation for using JPA with Hibernate.
- [Liquibase Documentation](https://www.liquibase.org/documentation/index.html) - Resources for managing database schema with Liquibase.
- [Postman Documentation](https://learning.postman.com/) - Official documentation for using Postman to test APIs.
- [Thymeleaf Documentation](https://www.thymeleaf.org/documentation.html) - Guide to using Thymeleaf for server-side rendering.
- [Docker Documentation](https://docs.docker.com/get-started/) - Getting started with Docker for containerization.