Your Ultimate Guide to Cerberus: A Stateless Token-Based Authorization System

Aug 12, 2023 | Programming

Welcome to the comprehensive guide on Cerberus, a RESTful token-based authorization system that seamlessly integrates JSON Web Tokens (JWT) and Spring Security. Whether you’re a developer eager to secure your API or a curious tech enthusiast, this article will walk you through the essential steps to set up and utilize Cerberus effectively.

Why Cerberus?

In our digital age, securing API endpoints is crucial. Cerberus provides a solution that adheres to REST principles, ensuring that all application states remain stateless on the server. To access its endpoints, users must authenticate and receive a JSON Web Token that serves as their ‘pass’ into the API. Imagine this as a special VIP pass at a concert—you need it to enter the backstage but it only works for specific areas based on your wristband color.

Requirements

  • Maven
  • Java 1.7 or greater

Getting Started with Cerberus

To launch Cerberus, simply open your terminal and execute the following command:

mvn spring-boot:run

If all goes well, Cerberus will be live at http://localhost:8080/api.

Built-in User Accounts

Cerberus comes with two user accounts for demonstration:

  • User: user:password
  • Admin: admin:admin

API Endpoints

Cerberus has two primary endpoints:

Authentication Process

To obtain a JWT token, you’ll send a POST request with your credentials. Here’s how you can do that using curl:

curl -i -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"admin"}' http://localhost:8080/api/auth

After a successful request, you will receive a token, which looks something like this:

token : eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiJ9.bKweskM-3QqOY8ScxhC9AcREOCG2UDY0Ylezdv1h81ALFg_v0QYBgxwfUjtf_Ns7RqAQIh_kFg1ZkeFV-szRUg

Using Your Token

Insert your token in the request header to access the protected endpoint:

curl -i -H "Content-Type: application/json" -H "X-Auth-Token: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiJ9.bKweskM-3QqOY8ScxhC9cREOCG2UDY0Ylezdv1h81ALFg_v0QYBgxwfUjtf_Ns7RqAQIh_kFg1ZkeFV-szRUg" -X GET http://localhost:8080/api/protected

If everything is correct, you’ll receive an HTTP 200 response.

Token Expiration and Refreshing

Tokens are designed to expire after a week to enhance security. However, you can refresh your token by sending a GET request to /api/auth/refresh before it expires. This refresh mechanism even works for tokens issued to mobile devices, ensuring you can always keep your token valid.

Testing with Cerberus

To run unit tests for Cerberus, execute the following command in your terminal:

mvn clean package

Troubleshooting Tips

If you encounter issues during setup or execution, consider the following:

  • Ensure you have Maven and Java 1.7 or later installed correctly.
  • Check that all dependencies are set up properly in your pom.xml.
  • Verify your curl commands for syntax errors, especially in headers and data format.
  • Make sure that no other services are blocking the desired port.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox