配置 Basic 身份认证
Basic 身份认证是保护 Web 服务的常见方式。它使用标准 HTTP Authorization 请求头和 Basic 方案, 后跟经过 Base64 编码的 username:password 字符串。
API7 网关提供 basic-auth插件来处理此身份认证流程,并根据消费者凭证数据库验证凭证。
本指南介绍标准配置流程。有关完整的插件字段参考和高级行为,请参阅 basic-auth。
前置条件
- API7 企业版实例正在运行。
- 已创建网关组且网关实例正在运行。
- 一个 Dashboard Token。
配置 Basic 身份认证
配置 Basic 身份认证包含三个步骤:
- 创建具有有效上游的服务。
- 创建启用
basic-auth的路由。 - 创建消费者并挂载 Basic Auth 凭据。
第 1 步:创建具有上游的服务
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/services/basic-auth-httpbin-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "basic-auth-httpbin-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'
adc.yaml
services:
- name: basic-auth-httpbin-service
upstream:
name: default
scheme: http
nodes:
- host: httpbin.org
port: 80
weight: 100
routes:
- name: basic-auth-route
uris:
- /anything/basic-auth
methods:
- GET
plugins:
basic-auth:
hide_credentials: true
consumers:
- username: basic-auth-consumer
credentials:
- name: basic-auth-consumer-cred
type: basic-auth
config:
username: alice
password: secret-password
adc sync -f adc.yaml