This project demonstrates a Spring Boot application with JWT authentication, deployed on Kubernetes with multiple environment configurations (development and production).
- Java 17 (Amazon Corretto recommended)
- Docker
- Kubernetes (Minikube)
- kubectl
- Maven
.
├── k8s/
│ ├── base/
│ │ ├── deployment.yaml
│ │ ├── service.yaml
│ │ ├── hpa.yaml
│ │ └── kustomization.yaml
│ └── overlays/
│ ├── development/
│ │ ├── kustomization.yaml
│ │ ├── namespace.yaml
│ │ └── patches/
│ │ └── deployment-patch.yaml
│ └── production/
│ ├── kustomization.yaml
│ ├── namespace.yaml
│ └── patches/
│ └── deployment-patch.yaml
└── spring-boot-with-jwt-authentication/
└── src/
└── main/
└── resources/
├── application.yml
├── application-development.yml
└── application-production.yml
Make sure you have Java 17 installed and configured:
# Set JAVA_HOME to Java 17
export JAVA_HOME=/path/to/your/java17
# Navigate to the application directory
cd spring-boot-with-jwt-authentication
# Build with Maven
./mvnw clean package -DskipTests
# Get the current commit hash
COMMIT_HASH=$(git rev-parse --short HEAD)
# Build the Docker image with commit hash as tag
docker build -t louishu/practice:${COMMIT_HASH} .
# Push to Docker Hub
docker push louishu/practice:${COMMIT_HASH}
Update the image tag in your Kustomize overlay files (k8s/overlays/development/kustomization.yaml
and k8s/overlays/production/kustomization.yaml
):
images:
- name: louishu/practice
newTag: ${COMMIT_HASH} # Replace with your actual commit hash
# Deploy using Kustomize
kubectl apply -k k8s/overlays/development
# Verify deployment
kubectl get all -n development
# Deploy using Kustomize
kubectl apply -k k8s/overlays/production
# Verify deployment
kubectl get all -n production
# Watch the pods
kubectl get pods -n <namespace> -w
# Check pod logs
kubectl logs -f <pod-name> -n <namespace>
# Check deployment status
kubectl describe deployment spring-boot-jwt -n <namespace>
The application comes with two pre-configured users:
- Regular User
- Username:
user
- Password:
password
- Role:
USER
- Username:
- Admin User
- Username:
admin
- Password:
password
- Role:
ADMIN
- Username:
curl -X POST http://localhost:8080/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"user","password":"password"}'
Response:
{
"token": "<JWT_TOKEN>"
}
curl http://localhost:8080/api/public
Response:
{
"message": "This is a public endpoint",
"timestamp": "1234567890"
}
curl http://localhost:8080/api/protected \
-H "Authorization: Bearer <your-jwt-token>"
Response:
{
"message": "This is a protected endpoint",
"username": "user",
"email": "[email protected]",
"role": "USER",
"timestamp": 1234567890
}
curl http://localhost:8080/api/admin \
-H "Authorization: Bearer <your-jwt-token>"
Response:
{
"message": "This is an admin endpoint",
"timestamp": "1234567890"
}
{
"timestamp": "2024-12-16 03:26:23",
"status": 401,
"error": "Unauthorized",
"message": "Please login first.",
"path": "/api/protected"
}
{
"timestamp": "2024-12-16 03:26:23",
"status": 403,
"error": "Forbidden",
"message": "Insufficient privileges.",
"path": "/api/admin"
}
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.