A secure backend service for managing books with admin login and two-factor authentication. You can log in as an admin, get a temporary access code using an authenticator app, and then add, view, or manage books.
- Admin login with password
- Two-step verification using Google Authenticator
- Add, update, delete, and view books
- Protected APIs that require login
- Swagger documentation (built-in testing UI)
- You log in with your username and password.
- If correct, you'll get a temporary token.
- You then open your Google Authenticator app and enter the 6-digit code.
- If the code is correct, you receive a final token.
- You use this final token to access all other APIs securely.
You can try the API using Swagger UI:
http://localhost:8080/swagger-ui.html
username: admin
password: password
Make sure to set up Google Authenticator using the secret from your environment variable (TOTP_SECRET
).
Here's an example book JSON you can POST after logging in:
{
"title": "Clean Code",
"author": "Robert C. Martin",
"isbn": "9780132350884",
"genre": "Programming",
"publishedDate": "2008-08-01",
"summary": "A handbook of agile software craftsmanship."
}
- Java 21 + Spring Boot 3
- MySQL (prod), H2 (test/dev)
- Spring Security with TOTP (2FA)
- Swagger (Springdoc) for API docs
- JUnit 5 + Mockito for testing
-
Clone the repo
-
Set environment variables:
ADMIN_USERNAME
,ADMIN_PASSWORD
TOTP_SECRET
(base32 string)SPRING_DATASOURCE_*
if using MySQLJWT_SECRET
,jwt.authExpirationMinutes
,jwt.otpExpirationMinutes
-
Run the app:
./mvnw spring-boot:run
dev
→ H2 in-memory DB, Swagger openprod
→ Use MySQL, JWT from env, secure settings
./mvnw test
Includes:
- Unit tests for service logic and tokens
- Integration tests for login + book APIs
This project supports Docker for local development.
Dockerfile
: builds the app into a secure imagedocker-compose.yml
: spins up the app + MySQL together.env.example
: shows the required environment variables.env
: your local config (never committed)
- Docker Desktop (Windows/Mac) or Docker CLI + Compose (Linux)
-
Copy the example env:
cp .env.example .env # Edit .env with your actual local secrets (or leave defaults for dev)
-
Start containers:
docker-compose up --build
-
Access:
- Swagger: http://localhost:8080/swagger-ui.html
- MySQL: running internally at
db:3306
docker-compose down # stop
docker-compose down -v # stop & delete DB volume (wipe data)