115 Star 698 Fork 166

GVPiresty / Apache APISIX

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
basic-auth.md 4.41 KB
一键复制 编辑 原始数据 按行查看 历史
homeward 提交于 2022-05-07 10:22 . docs: fix error link (#6989)
title keywords description
basic-auth
APISIX
Plugin
Basic Auth
basic-auth
This document contains information about the Apache APISIX basic-auth Plugin.

Description

The basic-auth Plugin is used to add basic access authentication to a Route or a Service.

This works well with a Consumer. Consumers of the API can then add their key to the header to authenticate their requests.

Attributes

For Consumer:

Name Type Required Description
username string True Unique username for a Consumer. If multiple Consumers use the same username, a request matching exception is raised.
password string True Password of the user.

For Route:

Name Type Required Default Description
hide_credentials boolean False false Set to true to pass the authorization request headers to the Upstream.

Enabling the Plugin

To enable the Plugin, you have to create a Consumer object with the authentication configuration:

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"
        }
    }
}'

You can also use the APISIX Dashboard to complete the operation through a web UI.

Once you have created a Consumer object, you can then configure a Route or a Service to authenticate requests:

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:1980": 1
        }
    }
}'

Example usage

After you have configured the Plugin as mentioned above, you can make a request to the Route as shown below:

curl -i -ufoo:bar http://127.0.0.1:9080/hello
HTTP/1.1 200 OK
...
hello, world

If the request is not authorized, an error will be thrown:

HTTP/1.1 401 Unauthorized
...
{"message":"Missing authorization in request"}

And if the user or password is not valid:

HTTP/1.1 401 Unauthorized
...
{"message":"Invalid user authorization"}

Disable Plugin

To disable the jwt-auth Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

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

搜索帮助