密钥认证
API 网关的主要作用是连接 API 消费者和提供者。出于安全原因,在允许消费者访问上游资源之前,它应该对消费者进行身份认证和授权。

APISIX 具有灵活的插件扩展系统,并拥有许多用于用户身份认证和授权的现有插件。例如:
- Key Authentication
- Basic Authentication
- HMAC
- JSON Web Token (JWT) Authentication
- OpenID Connect
- Keycloak Authorization
- Casdoor Authorization
- Casbin Authorization
- Open Policy Agent (OPA)
- Wolf RBAC
- Central Authentication Service (CAS)
- LDAP
- Forward Authentication
在本教程中,你将创建一个消费者,使用密钥认证配置其凭证,并学习如何启用和禁用密钥认证。
关键概念
消费者
消费者 是指使用 API 的应用程序或开发者。
在 APISIX 中,创建消费者需要一个唯一的 username。作为密钥认证配置的一部分,你还需要将上述列表中的身份认证插件之一添加到消费者的 plugin 字段中。
密钥认证
密钥认证是一种相对简单但广泛使用的身份认证方法。其思路如下:
- 管理员将身份认证插件添加到路由。
- API 消费者在发送请求时,将密钥附加到查询字符串或请求头中以进行身份认证。
前置条件
- 完成 安装 APISIX 以在 Docker 或 Kubernetes 中安装 APISIX。
- 完成 配置路由。
- 如果使用这些工具,请安装 ADC 或 APISIX-MCP。
配置密钥认证
- Admin API
- ADC
- APISIX-MCP
创建消费者
创建消费者 tom:
请在生产环境中使用复杂的密钥。
curl -i "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT -d '
{
"username": "tom"
}'
如果消费者创建成功,你将收到 HTTP/1.1 201 Created 响应。
配置消费者凭证
为 tom 配置 key-auth 凭证:
curl "http://127.0.0.1:9180/apisix/admin/consumers/tom/credentials" -X PUT -d '
{
"id": "cred-tom-key-auth",
"plugins": {
"key-auth": {
"key": "secret-key"
}
}
}'
如果消费者凭证创建成功,你将收到 HTTP/1.1 201 Created 响应。
启用身份认证
更新 配置路由 中的 getting-started-ip 路由以添加 key-auth 插件:
curl -i "http://127.0.0.1:9180/apisix/admin/routes/getting-started-ip" -X PATCH -d '
{
"plugins": {
"key-auth": {}
}
}'
如果路由更新成功,你将收到 HTTP/1.1 200 OK 响应。
创建一个包含消费者和路由的 ADC 配置文件:
consumers:
- username: tom
credentials:
- name: tom-key
type: key-auth
config:
key: secret-key
services:
- name: httpbin Service
routes:
- uris:
- /ip
name: getting-started-ip
plugins:
key-auth: {}
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到 APISIX:
adc sync -f adc.yaml
在你的 AI 客户端中输入以下提示:
Create a consumer named tom in APISIX and the key-auth credential for the consumer should be secret-key.
Enable the key-auth plugin on the route getting-started-ip, requiring consumers to provide an API key when accessing that route.
你应该看到类似的响应:
Created consumer 'tom' with key-auth credential and enabled key-auth plugin on route 'getting-started-ip'. The consumer can now access the route using API key 'secret-key' in either the 'apikey' header or query parameter.
验证
你将在本节中验证密钥认证是否已成功启用。
- Admin API
- ADC
- APISIX-MCP
发送不带 Key 的请求
发送不带 apikey 头的请求。
curl -i "http://127.0.0.1:9080/ip"
由于未提供密钥,你将收到未授权的 HTTP/1.1 401 Unauthorized 响应。
发送携带错误 Key 的请求
发送在 apikey 头中带有错误密钥的请求。
curl -i "http://127.0.0.1:9080/ip" -H 'apikey: wrong-key'
由于密钥不正确,你将收到 HTTP/1.1 401 Unauthorized 响应。
发送携带正确 Key 的请求
发送在 apikey 头中带有正确密钥的请求。
curl -i "http://127.0.0.1:9080/ip" -H 'apikey: secret-key'
由于提供了正确的密钥,你将收到 HTTP/1.1 200 OK 响应。
发送不带 Key 的请求
发送不带 apikey 头的请求。
curl -i "http://127.0.0.1:9080/ip"
由于未提供密钥,你将收到未授权的 HTTP/1.1 401 Unauthorized 响应。
发送携带错误 Key 的请求
发送在 apikey 头中带有错误密钥的请求。
curl -i "http://127.0.0.1:9080/ip" -H 'apikey: wrong-key'
由于密钥不正确,你将收到 HTTP/1.1 401 Unauthorized 响应。
发送携带正确 Key 的请求
发送在 apikey 头中带有正确密钥的请求。
curl -i "http://127.0.0.1:9080/ip" -H 'apikey: secret-key'
由于提供了正确的密钥,你将收到 HTTP/1.1 200 OK 响应。
发送不带 Key 的请求
在你的 AI 客户端中输入以下提示:
Send a request to the route without any authentication header.
你应该看到类似的响应:
Unauthenticated request to '/ip' returned 401 as expected
发送携带错误 Key 的请求
在你的 AI 客户端中输入以下提示:
Send a request to the route with wrong-key.
你应该看到类似的响应:
Invalid API key returns 401 (wrong credentials)
发送携带正确 Key 的请求
在你的 AI 客户端中输入以下提示:
Send a request to the route with secret-key.
你应该看到类似的响应:
Valid API key 'secret-key' authenticates successfully (200)
禁用身份认证
通过将 _meta.disable 参数设置为 true 来禁用密钥认证插件。
- Admin API
- ADC
- APISIX-MCP
curl "http://127.0.0.1:9180/apisix/admin/routes/getting-started-ip" -X PATCH -d '
{
"plugins": {
"key-auth": {
"_meta": {
"disable": true
}
}
}
}'
发送不带任何密钥的请求以进行验证:
curl -i "http://127.0.0.1:9080/ip"
由于密钥认证已禁用,你将收到 HTTP/1.1 200 OK 响应。
consumers:
- username: tom
plugins:
key-auth:
key: secret-key
services:
- name: httpbin Service
routes:
- uris:
- /ip
name: getting-started-ip
plugins:
key-auth:
_meta:
disable: true
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到 APISIX:
adc sync -f adc.yaml
发送不带任何密钥的请求以进行验证:
curl -i "http://127.0.0.1:9080/ip"
由于密钥认证已禁用,你将收到 HTTP/1.1 200 OK 响应。
在你的 AI 客户端中输入以下提示:
Disable the key authentication plugin for the route getting-started-ip while keeping its configuration intact.
你应该看到类似的响应:
Successfully disabled the key-auth plugin for route 'getting-started-ip' while maintaining all other configuration. The route now:
* Still matches requests to URI '/ip'
* Forwards to upstream httpbin.org:80
* No longer requires API key authentication
Retains all other settings including load balancing configuration
在你的 AI 客户端中发送不带任何密钥的请求:
Send a request to the route without any key.
你应该看到类似的响应:
The request to the /ip route without an API key was successful (HTTP 200).
下一步
你已经学习了如何为路由配置密钥认证。在下一个教程中,你将学习如何配置限流限速。