REST API built with Spring Boot that implements JWT authentication and role-based access control.
Java 21 · Spring Boot 3.x · Maven · Docker · Spring Security · jjwt
Build the image:
docker build -t auth-api .Run the container:
docker run -p 8080:8080 \
-e JWT_SECRET=your-secret-key-min-32-characters \
-e ADMIN_MAIL=admin@example.com \
-e ADMIN_PASSWORD=your-admin-password \
auth-apiRequirements: Java 21, Maven
- Clone the repository
- Set the required environment variables (see Configuration)
- Run the application:
./mvnw spring-boot:runThe following environment variables are required:
| Variable | Description | Example |
|---|---|---|
JWT_SECRET |
Secret key for JWT signing (min. 32 characters) | my-super-secret-key-32-chars!! |
ADMIN_MAIL |
Email for the default admin user | admin@example.com |
ADMIN_PASSWORD |
Password for the default admin user | admin123 |
You can check application.properties.example for reference.
| Method | Endpoint | Description | Body |
|---|---|---|---|
| POST | /api/auth/register |
Register a new user | { "email": "", "password": "" } |
| POST | /api/auth/login |
Login and get JWT token | { "email": "", "password": "" } |
Both endpoints return:
{
"token": "eyJhbGci..."
}Include the token in the Authorization header: Bearer <token>
| Method | Endpoint | Role required | Description |
|---|---|---|---|
| GET | /api/demo |
USER, ADMIN | Returns a demo message |
| GET | /api/demo/admin |
ADMIN only | Returns an admin-only message |
WeakKeyException on startup
Your JWT_SECRET is too short. It must be at least 32 characters.
401 Unauthorized on protected endpoints
Make sure you are sending the token in the Authorization header with the format Bearer <token>.
Port already in use
Another process is using port 8080. Stop it or change the port with -p 9090:8080 in the Docker run command.