Skip to main content
Version: 3.10.x

Access Control Lists

Access control lists restrict API access using the consumer-restriction plugin. The plugin can match on consumer name, service ID, or route ID, and supports both whitelists and blacklists. The example below uses consumer-name matching, which is the default and the most common pattern.

The inherited gateway schema accepts consumer_group_id, but API7 Gateway does not expose a customer-manageable consumer-group resource or assign a group ID to consumers. Do not use this identifier type: the value is absent and the plugin rejects the request with HTTP 401.

Capabilities​

  • Multiple identifier types: Match on consumer name (default), service ID, or route ID via the plugin's type field.
  • Whitelist and blacklist: Explicitly allow or deny traffic from the configured identifiers.
  • Plugin integration: Combine with authentication plugins (e.g., key-auth, jwt-auth) so the gateway knows which consumer made the request.
  • Granular control: Attach access controls to a route, a service, or a consumer.

Prerequisites​

  1. Create consumers named authorized-consumer and consumer-override (see Tutorial: Proxying and Managing API Requests via Plugins).
  2. Add a key-auth credential to each Consumer and record the two keys for verification.
  3. Prepare a token from the Dashboard.

Configure an Access Control List​

To restrict access to a route, configure the consumer-restriction plugin on the route alongside an authentication plugin.

Create the Service with a public echo upstream:

curl -k "https://localhost:7443/apisix/admin/services/acl-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "acl-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'

Create the restricted route on the Service:

curl -k "https://localhost:7443/apisix/admin/routes/acl-restricted-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "acl-restricted-route",
"service_id": "acl-service",
"paths": ["/anything/*"],
"plugins": {
"key-auth": {},
"consumer-restriction": {
"whitelist": [
"authorized-consumer"
]
}
}
}'

Replace {gateway_group_id} with the gateway group ID from the Gateway Groups page in the Dashboard (use default for the gateway group created by the quickstart). The verification and override steps use the literal Service and Consumer names shown above.

Verify the Access Policy​

Set the gateway address and the two keys created in the prerequisites:

export API7_GATEWAY="http://127.0.0.1:9080"
export AUTHORIZED_CONSUMER_KEY="replace-with-authorized-consumer-key"
export CONSUMER_OVERRIDE_KEY="replace-with-consumer-override-key"

First, send a request without a credential:

curl -i "$API7_GATEWAY/anything/example"

The authentication plugin rejects the request with HTTP 401 Unauthorized because it cannot identify a Consumer.

Next, send a request as consumer-override, which is not in the Route whitelist:

curl -i "$API7_GATEWAY/anything/example" \
-H "apikey: $CONSUMER_OVERRIDE_KEY"

The gateway identifies the Consumer, and consumer-restriction rejects it with HTTP 403 Forbidden.

Finally, use the authorized Consumer:

curl -i "$API7_GATEWAY/anything/example" \
-H "apikey: $AUTHORIZED_CONSUMER_KEY"

The request passes both plugins and reaches httpbin.org, which returns HTTP 200 OK for this path.

Override the Route Policy for One Consumer​

Plugins configured on a Consumer are applied after authentication. If the Consumer and Route configure the same plugin, the Consumer configuration replaces the Route configuration as a whole for that Consumer. The gateway does not intersect or join the two whitelists.

Add consumer-restriction to consumer-override and allow its own username:

curl -k "https://localhost:7443/apisix/admin/consumers/consumer-override?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"username": "consumer-override",
"plugins": {
"consumer-restriction": {
"whitelist": [
"consumer-override"
]
}
}
}'

Repeat the request with CONSUMER_OVERRIDE_KEY. It now reaches the upstream even though the Route whitelist still contains only authorized-consumer, because the Consumer configuration is the effective consumer-restriction configuration for this request.

A consumer-restriction Global Rule is evaluated separately and is not replaced by the Consumer configuration. If you configure the plugin in a Global Rule as well, the request must pass both the Global Rule and the effective Route, Service, or Consumer configuration.

To restore the original behavior after testing, update consumer-override without the Consumer-level plugin:

curl -k "https://localhost:7443/apisix/admin/consumers/consumer-override?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"username": "consumer-override"
}'

Key Configuration Parameters​

FieldTypeDescription
typestringWhat whitelist/blacklist entries refer to. Use consumer_name (default), service_id, or route_id. Do not use the inherited consumer_group_id option; API7 Gateway does not assign group IDs and rejects the request with HTTP 401.
whitelistarray[string]Identifiers that are allowed access. The accepted identifier kind is determined by type.
blacklistarray[string]Identifiers that are denied access. The accepted identifier kind is determined by type.
rejected_codeintegerThe HTTP status code to return when a request is rejected (default 403).
rejected_msgstringThe error message to return when a request is rejected.

For configuration patterns beyond consumer-name matching (such as restricting which consumers can access a given service or route, or restricting allowed HTTP methods per consumer), see the consumer-restriction plugin reference.

Implementation Patterns​

Pattern 1: Whitelisting for Private Services​

Use a whitelist to allow only specific internal consumers or partners to access private APIs. All other authenticated consumers will be rejected.

Pattern 2: Blacklisting Malicious Consumers​

Use a blacklist to block specific consumers that have been identified as potentially malicious or are violating usage terms, while allowing all other authenticated consumers.

Next Steps​