Welcome to MyCoolService! This project showcases the latest in software development trends by combining the power of reactive programming with state-of-the-art security features. Built using Spring Boot 3, Spring Security, and Spring WebFlux, MyCoolService delivers high-performance, scalable, and secure RESTful services, perfect for modern applications.
- Reactive Programming with WebFlux: Utilizes Spring WebFlux to create non-blocking, event-driven RESTful services, ensuring high throughput and responsiveness.
- Advanced Security:
- Spring Security: Implements the 8000 latest Spring Security features for robust authentication.
- JWT Tokens: Uses JSON Web Tokens (JWT) for secure and stateless authentication.
- Open Policy Agent (OPA): Integrates OPA for flexible and powerful authorization policies.
- Spring Boot 3: The backbone of the application, providing ease of setup and configuration with the latest features and improvements.
- Spring WebFlux: A reactive, non-blocking web framework that enables the creation of responsive and resilient services.
- Spring Security: Industry-standard framework, powerful & highly customizable authentication and access-control with protection against common attacks. The de-facto standard for securing Spring-based applications.
- JWT (JSON Web Tokens): A compact, URL-safe means of representing claims to be transferred between two parties.
- OPA (Open Policy Agent): A policy engine that decouples policy decision-making from your application, enabling dynamic and fine-grained authorization.
Ensure you have the following installed:
- JDK 17 or higher
- Maven 3.9.6 or higher
- Docker (optional, for containerized deployment)
- Minikube (optional, for k8 deployment & orchestration)
- Clone the repository:
git clone https://github.com/Karan-patel/mycoolservice.git
cd mycoolservice
- Build the application:
mvn clean install
- Run the application:
mvn spring-boot:run
To deploy the application using Docker, follow these steps:
- Build the Docker image:
docker build -t mycoolservice-v1:latest .
- Run the Docker container:
docker run -p 8080:8080 mycoolservice-v1:latest
- Deploy into minikube: Now since we have container that runs and exposes port 8080, all we require to run in minikube is deployment.yaml. Speaking of Kubernetes, here comes the magic ! :) kubectl will do it for us. Run the following commands !
kubectl create namespace mycoolservice
minikube image load mycoolservice-v1:latest
kubectl create deployment mycoolservice --image=mycoolservice-v1:latest --dry-run=client -o=yaml > deployment.yaml -n mycoolservice
echo --- >> deployment.yaml
kubectl create service clusterip mycoolservice --tcp=8080:8080 --dry-run=client -o=yaml >> deployment.yaml -n mycoolservice
kubectl apply -f deployment.yaml -n mycoolservice
- Time to check if application is running !
kubectl get all -n mycoolservice
- To connect with application running in container exposed as service, sometimes need SSH-tunneling Better to use Ingress addon for minikube.
kubectl port-forward svc/mycoolservice 8080:8080 -n mycoolservice
- Final step
curl localhost:8080/actuator/health
- For OPA based policy authorization deploy opa container To load OPA with above policy in Minikube follow guideline here. Git repository for detailed guide to load policy in Open Policy Agent (OPA) and deploy in Minikube.
git clone https://github.com/Karan-patel/mycoolservice.git
Note : This service use inmemory db to store user credentials, you can configure user, role and password into SecurityConfig.java. Default, I have added 2 users swisscom, user with role admin, user resp. This will be required to fetch authorization token as explained below.
The application exposes a set of RESTful endpoints. Here are a few examples:
- GET /mycoolservice/authenticate: To obtain a jwt token for authentication and authorization.
curl --location 'http://localhost:8080/mycoolservice/authenticate' \
--header 'Content-Type: application/json' \
--data '{
"username" : "user",
"password" : "password"
}'
Output :
eyJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbiJdLCJzdWIiOiJzd2lzc2NvbSIsImlhdCI6MTcxNzE2OTM0MywiZXhwIjoxNzE3MjA1MzQzfQ.NU3TkVFkigKwtzf07KRvnPtC2vvaqkvq5DK9VOOxMSw
Note : This token generation API is provided for demonstration/testing purposes only. In a real-world scenario, login credentials should be encrypted and securely transmitted over HTTPS to ensure the security of user data.
- POST /mycoolservice/api/users: Creates a new user.
curl --location 'http://localhost:8080/mycoolservice/api/users' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbiJdLCJzdWIiOiJzd2lzc2NvbSIsImlhdCI6MTcxNzE2OTM0MywiZXhwIjoxNzE3MjA1MzQzfQ.NU3TkVFkigKwtzf07KRvnPtC2vvaqkvq5DK9VOOxMSw' \
--data-raw '{
"name":"John wick",
"email":"John@matrix.com"
}'
- GET /mycoolservice/api/users: Get list of user.
curl --location 'http://localhost:8080/mycoolservice/api/users' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbiJdLCJzdWIiOiJzd2lzc2NvbSIsImlhdCI6MTcxNzE2OTM0MywiZXhwIjoxNzE3MjA1MzQzfQ.NU3TkVFkigKwtzf07KRvnPtC2vvaqkvq5DK9VOOxMSw'
- Authentication: Users authenticate using JWT tokens. Obtain a token by providing valid credentials to the
/mycoolservice/authenticate
endpoint. - Authorization: All endpoints are secured using OPA policies, ensuring fine-grained access control.
Authorization policy
- Only users with "admin" role can create new user
- Only authenticated users can read the list of users
- Everyone else has no access
We welcome contributions! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Create a new Pull Request.
For any questions or feedback, please reach out to Karan Patel.
By leveraging the power of reactive programming, advanced security mechanisms, and modern authorization frameworks, MyCoolService is designed to be a high-performance, secure, and scalable solution for your RESTful service needs. Enjoy building with the latest tech trends!