a7-plugin-consumer-restriction
Overview
The consumer-restriction plugin in API7 Enterprise Edition (API7 EE) restricts access to routes or services based
on the authenticated consumer's identity. It supports three restriction types
and three matching modes (blacklist, whitelist, method-level).
Priority: 2400 (runs in the access phase after authentication plugins).
Prerequisite: MUST be paired with an authentication plugin (key-auth,
basic-auth, jwt-auth, hmac-auth, wolf-rbac, etc.) to identify the
consumer.
When to Use
- Restrict specific routes to certain consumers.
- Implement tiered access (free vs premium consumers).
- Control which HTTP methods each consumer can use.
- Restrict consumers to specific services or routes.
Plugin Configuration Reference
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
type | string | No | consumer_name | Restriction type: consumer_name, service_id, route_id |
whitelist | array[string] | One of three* | — | Allowed identifiers |
blacklist | array[string] | One of three* | — | Blocked identifiers |
allowed_by_methods | array[object] | One of three* | — | Per-consumer HTTP method restrictions |
allowed_by_methods[].user | string | No | — | Consumer username |
allowed_by_methods[].methods | array[string] | No | — | Allowed HTTP methods: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, CONNECT, TRACE, PURGE |
rejected_code | integer | No | 403 | HTTP status code for rejected requests (≥ 200) |
rejected_msg | string | No | "The {type} is forbidden." | Custom rejection message |
* At least one of whitelist, blacklist, or allowed_by_methods is required.
Evaluation Priority
blacklist (highest) > whitelist > allowed_by_methods (lowest)
- Blacklist: if consumer matches → 403 immediately.
- Whitelist: if consumer NOT in whitelist → blocked (unless allowed_by_methods permits).
- allowed_by_methods: if consumer's method not in allowed list → blocked.
Restriction Type Placement
| Type | Configure On | Description |
|---|---|---|
consumer_name | Route/Service | Restrict which consumers can access this route |
service_id | Consumer | Restrict which services this consumer can access |
route_id | Consumer | Restrict which routes this consumer can access |
Step-by-Step Examples
1. Whitelist by Consumer Name
Only allow jack1 to access the route:
# Create consumers with auth
a7 consumer create --gateway-group default -f - <<'EOF'
{
"username": "jack1",
"plugins": {
"key-auth": {"key": "jack1-key"}
}
}
EOF
a7 consumer create --gateway-group default -f - <<'EOF'
{
"username": "jack2",
"plugins": {
"key-auth": {"key": "jack2-key"}
}
}
EOF
# Create route with restriction
a7 route create --gateway-group default -f - <<'EOF'
{
"id": "restricted",
"uri": "/api/*",
"plugins": {
"key-auth": {},
"consumer-restriction": {
"whitelist": ["jack1"]
}
},
"upstream": {
"type": "roundrobin",
"nodes": [{"host": "backend", "port": 8080, "weight": 1}]
}
}
EOF
curl -H 'apikey: jack1-key' /api/data→ 200 OKcurl -H 'apikey: jack2-key' /api/data→ 403{"message":"The consumer_name is forbidden."}