This repository demonstrates how to integrate MongoDB Queryable Encryption with Spring Data MongoDB. It covers essential practices for creating encrypted collections and querying encrypted data efficiently using MongoDB’s Queryable Encryption capabilities. The project includes sample code for:
- Creating encrypted collections and data encryption keys (DEKs).
- Running range and equality queries on encrypted fields.
- Working with MongoRepository and MongoTemplate in a secure, encrypted context.
- Java 17 or higher (tested with Corretto 21)
- Maven
- MongoDB 8.0+ with Queryable Encryption support
- MongoDB crypt_shared library ->
Dowload the Automatic Encryption Shared Library
Open your terminal and run:
export MONGODB_URI="<YOUR_CONNECTION_STRING>" CRYPT_PATH="<PATH_TO_AUTOMATIC_ENC_SHA_LIB>"
mvn spring-boot:run
Once the application is running, access:
http://localhost:8080/swagger-ui.html
Or (for some versions):
http://localhost:8080/swagger-ui/index.html
It includes endpoints for:
- /employees → Manage employees records
Create a Employee
POST /employees
Find All employees
GET /employees
Find employees with age greater than a specified value
GET /employees/ages/greater-than?age=50
This project includes ready-to-use .http files to test all API endpoints easily from your IDE (such as IntelliJ IDEA, Rider, or VSCode with REST Client plugin).
- employee_requests.http → Test all endpoints under /employees
- Open the .http file in IntelliJ.
- Click on the green "Run" icon next to any request.
- You’ll see the response in a built-in HTTP client tab.