1 Star 0 Fork 166

yinren / Apache APISIX

forked from iresty / Apache APISIX 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
basic-auth.md 3.71 KB
一键复制 编辑 原始数据 按行查看 历史

目录

名字

basic-auth 是一个认证插件,它需要与 consumer 一起配合才能工作。

添加 Basic Authentication 到一个 serviceroute。 然后 consumer 将其用户名和密码添加到请求头中以验证其请求。

有关 Basic Authentication 的更多信息,可参考 维基百科 查看更多信息。

属性

  • username: 不同的 consumer 对象应有不同的值,它应当是唯一的。不同 consumer 使用了相同的 username ,将会出现请求匹配异常。
  • password: 用户的密码

如何启用

1. 创建一个 consumer 对象,并设置插件 basic-auth 的值。

curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "username": "foo",
    "plugins": {
        "basic-auth": {
            "username": "foo",
            "password": "bar"
        }
    }
}'

你可以使用浏览器打开 dashboard:http://127.0.0.1:9080/apisix/dashboard/,通过 web 界面来完成上面的操作,先增加一个 consumer: auth-1

然后在 consumer 页面中添加 basic-auth 插件: auth-2

2. 创建 Route 或 Service 对象,并开启 basic-auth 插件。

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "methods": ["GET"],
    "uri": "/hello",
    "plugins": {
        "basic-auth": {}
    },
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:8080": 1
        }
    }
}'

Test Plugin

  • 缺少 Authorization header
$ curl http://127.0.0.2:9080/hello -i
HTTP/1.1 401 Unauthorized
...
{"message":"Missing authorization in request"}
  • 用户名不存在:
$ 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:foo http://127.0.0.1:9080/hello
HTTP/1.1 401 Unauthorized
...
{"message":"Password is error"}
...
  • 成功请求:
$ curl -i -ufoo:bar http://127.0.0.1:9080/hello
HTTP/1.1 200 OK
...
hello, foo!
...

禁用插件

当你想去掉 basic-auth 插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:

$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "methods": ["GET"],
    "uri": "/hello",
    "plugins": {},
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:8080": 1
        }
    }
}'
Lua
1
https://gitee.com/liujunao153/apisix.git
git@gitee.com:liujunao153/apisix.git
liujunao153
apisix
Apache APISIX
master

搜索帮助