Zuplo

API Key Authentication

Secure Your Javalin API with API Keys

The Zuplo API Gateway protects your backend API from unauthorized access, abuse, and overload. Add API key authentication to your Javalin API in minutes.

Secure Access

Authenticate every request before it reaches your backend.

Control Costs

Set rate limits and quotas to prevent runaway usage.

Ensure Reliability

Shield your backend from overload with traffic management.

How it works

The Zuplo API Gateway sits between your clients and your Javalin backend, providing a secure layer of protection and control.

Client
Zuplo API Gateway

Customer VPC

Backend

Step-by-step tutorial

It takes only a few minutes to put Zuplo in front of your Javalin backend, adding API key authentication, and configuring your origin to trust requests from Zuplo using shared secrets.

1

Create a Route in Zuplo

First, create a new route in your Zuplo project that will proxy requests to your Javalin backend. This route will be the entry point for your API consumers.

Creating a route in Zuplo
Learn how to create routes

📄 OpenAPI native. Import your existing OpenAPI spec to instantly create routes and power your API documentation .

2

Add API Key Authentication Policy

Add the API Key Authentication policy to your route. This policy validates incoming API keys and ensures only authorized consumers can access your API.

Adding API Key Authentication policy in Zuplo
API Key Authentication docs

🔐 Leaked key? No problem. As a GitHub Secret Scanning partner, Zuplo can automatically revoke exposed keys before they can be exploited.

3

Add Set Header Policy

Add the Set Header policy to your route. This policy sets the shared secret header on requests to ensure only requests from Zuplo are accepted by your API.

Adding Set Header policy in Zuplo
Set Header Policy docs
4

Secure Your Javalin API with the Shared Secret

Configure your Javalin backend to validate the shared secret header set by Zuplo. This ensures that only requests coming through your Zuplo gateway are accepted.

Java/Kotlin
import io.javalin.Javalin;
import io.javalin.http.Context;
import io.javalin.http.Handler;
import io.javalin.http.HttpResponseException;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Optional;

public class SecureApiExample {

    public static void main(String[] args) {
        String expectedSecret = Optional.ofNullable(System.getenv("SHARED_SECRET"))
                                        .orElseThrow(() -> new RuntimeException("Server configuration error"));

        Javalin app = Javalin.create().start(7000);

        app.before("/protected/*", new SharedSecretValidator(expectedSecret));

        app.get("/protected/endpoint", ctx -> {
            ctx.json(new Message("Access granted"));
        });
    }

    static class SharedSecretValidator implements Handler {

        private final String expectedSecretHash;

        SharedSecretValidator(String expectedSecret) {
            this.expectedSecretHash = hashSecret(expectedSecret);
        }

        @Override
        public void handle(Context ctx) {
            String secret = ctx.header("x-shared-secret");

            if (secret == null) {
                throw new HttpResponseException(401, "No secret provided");
            }

            String providedSecretHash = hashSecret(secret);
            if (!timingSafeEqual(providedSecretHash, expectedSecretHash)) {
                throw new HttpResponseException(401, "Invalid secret");
            }
        }

        private String hashSecret(String secret) {
            try {
                MessageDigest digest = MessageDigest.getInstance("SHA-256");
                byte[] hash = digest.digest(secret.getBytes(StandardCharsets.UTF_8));
                return new String(hash, StandardCharsets.UTF_8);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException("Error hashing secret", e);
            }
        }

        private boolean timingSafeEqual(String a, String b) {
            byte[] bytesA = a.getBytes(StandardCharsets.UTF_8);
            byte[] bytesB = b.getBytes(StandardCharsets.UTF_8);
            return MessageDigest.isEqual(bytesA, bytesB);
        }
    }

    static class Message {
        private String message;

        Message(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }
    }
}
5

Call Your API Through Zuplo

Now you can call your API through Zuplo using an API key. The request will be authenticated at the gateway, and securely forwarded to your Javalin backend.

Terminalbash
curl -X GET \
  'https://your-api.zuplo.dev/your-route' \
  -H 'Authorization: Bearer YOUR_API_KEY'

Ready to secure your API?

Get started with Zuplo for free and add API key authentication to your Javalin API in minutes.