basic-auth
名字
添加 Basic Authentication 到一个 service
或 route
。 然后 consumer
将其用户名和密码添加到请求头中以验证其请求。
如何启用
你可以使用浏览器打开 dashboard:http://127.0.0.1:9080/apisix/dashboard/
,通过 web 界面来完成上面的操作,先增加一个 consumer:
然后在 consumer 页面中添加 basic-auth 插件:
2. 创建 Route 或 Service 对象,并开启 basic-auth
插件。
{
"methods": ["GET"],
"uri": "/hello",
"plugins": {
"basic-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1
}
}'
- 缺少 Authorization header
- 用户名不存在:
$ curl -i -ubar:bar http://127.0.0.1:9080/hello
HTTP/1.1 401 Unauthorized
...
{"message":"Invalid user key in authorization"}
- 密码错误:
- 成功请求:
$ curl -i -ufoo:bar http://127.0.0.1:9080/hello
HTTP/1.1 200 OK
...
...
禁用插件
当你想去掉 basic-auth
插件的时候,很简单,在插件的配置中把对应的 配置删除即可,无须重启服务,即刻生效: