Meta description: Secure and throttle your APIs with Kong: key-auth, Basic auth, JWT, ACL, rate limiting, request size limiting and IP restriction.
What you'll learn
- How plugins work and where they apply
- Authentication vs authorization
- Ways to authenticate
- How to limit traffic fairly
What is a plugin?
A plugin adds behavior to the request/response flow without changing your backend code. You can attach it at different scopes:
| Scope | Applies to |
|---|---|
| Global | Every request through Kong |
| Service | All routes of one service |
| Route | Only one route |
| Consumer | Only one caller |
When scopes overlap, the more specific one wins.
Part 1: Authentication, "who are you?"
Key authentication (key-auth)
The simplest approach. The client sends a secret key in a header.
# 1. Enable on a service
curl -X POST http://localhost:8001/services/alpha-service/plugins --data name=key-auth
# 2. Create a key for the consumer
curl -X POST http://localhost:8001/consumers/asha/key-auth --data key=asha-secret-key
# 3. Call
curl -i http://localhost:8000/alpha/hello # 401
curl -i http://localhost:8000/alpha/hello -H 'apikey: asha-secret-key' # 200
Without a key, Kong answers 401 and your backend never sees the request.
Basic authentication (basic-auth)
Username and password in the Authorization header. Simple and widely supported, but use it only over HTTPS.
JWT authentication (jwt)
The client sends a signed token: Authorization: Bearer <token>.
curl -X POST http://localhost:8001/services/alpha-service/plugins --data name=jwt
curl -X POST http://localhost:8001/consumers/asha/jwt # returns key + secret
Kong checks the token's signature with the consumer's secret and checks it hasn't expired. The token's iss claim must equal the consumer's JWT key.
| Method | Best for |
|---|---|
| key-auth | Simple service-to-service or partner access |
| basic-auth | Quick internal tools (over HTTPS) |
| jwt | User sessions, stateless tokens with expiry |
Part 2: Authorization, "what may you do?" (acl)
Authentication identifies the caller. Authorization decides what they can access. Kong's ACL plugin uses groups:
# put the consumer in a group
curl -X POST http://localhost:8001/consumers/asha/acls --data group=shoppers
# allow only admins on a route
curl -X POST http://localhost:8001/routes/alpha-write/plugins \
--data name=acl --data 'config.allow=admins'
The ACL plugin needs an authentication plugin on the same route, because it has to know who the consumer is.
401 vs 403. 401 means "I don't know who you are". 403 means "I know you, but you may not do this."
Part 3: Traffic control, "how much?"
Rate limiting
Protects your service from abuse and accidental loops.
curl -X POST http://localhost:8001/services/alpha-service/plugins \
--data name=rate-limiting \
--data config.minute=30 \
--data config.policy=local
Responses include headers such as X-RateLimit-Remaining-Minute. After the limit, Kong returns 429 Too Many Requests.
Which policy?
-
local: counters in each Kong node's memory (fast, but each node counts alone) -
redis: shared counters across nodes (accurate across a cluster)
You can also attach a rate limit to a consumer, so a premium customer gets 1000/min and a free one gets 30/min.
Request size limiting
curl -X POST http://localhost:8001/services/alpha-service/plugins \
--data name=request-size-limiting \
--data config.allowed_payload_size=1 \
--data config.size_unit=megabytes
Oversized bodies are rejected with 413 before they reach your backend.
IP restriction
Allow or deny by address, handy for admin routes:
curl -X POST http://localhost:8001/routes/alpha-write/plugins \
--data name=ip-restriction --data 'config.allow=10.0.0.0/8'
A typical policy for one API
| Route | Plugins |
|---|---|
GET /items |
auth, group readers or admins
|
POST /items |
auth, group admins only |
/orders |
auth, ACL, rate limit 30/min |
| everything | request size limit 1 MB |
Common mistakes
- Adding
aclwithout an authentication plugin. - Using
rate-limitingwithlocalpolicy on several Kong nodes and expecting a global total. - Committing real secrets and keys to git. Use environment variables or a secret manager.
- Confusing 401 and 403 when debugging.
Key takeaways
- Plugins give you security and limits without touching backend code.
- Authenticate first, authorize second.
- Rate limits and size limits are cheap protection; set them early.
- Attach plugins at the narrowest scope that makes sense.
Next: Transformations and observability.
Top comments (0)