1 Star 0 Fork 0

lad/apisix

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
apisix
benchmark
bin
ci
conf
docs
ar
assets
en/latest
architecture-design
discovery
examples
plugins
api-breaker.md
authz-keycloak.md
basic-auth.md
batch-requests.md
consumer-restriction.md
cors.md
dubbo-proxy.md
echo.md
error-log-logger.md
ext-plugin-post-req.md
ext-plugin-pre-req.md
fault-injection.md
grpc-transcode.md
hmac-auth.md
http-logger.md
ip-restriction.md
jwt-auth.md
kafka-logger.md
key-auth.md
limit-conn.md
limit-count.md
limit-req.md
log-rotate.md
mqtt-proxy.md
node-status.md
openid-connect.md
prometheus.md
proxy-cache.md
proxy-mirror.md
proxy-rewrite.md
redirect.md
referer-restriction.md
request-id.md
request-validation.md
response-rewrite.md
server-info.md
serverless.md
skywalking.md
sls-logger.md
syslog.md
tcp-logger.md
traffic-split.md
udp-logger.md
uri-blocker.md
wolf-rbac.md
zipkin.md
FAQ.md
admin-api.md
aws.md
batch-processor.md
benchmark.md
config.json
control-api.md
customize-nginx-configuration.md
debug-function.md
discovery.md
external-plugin.md
getting-started.md
grpc-proxy.md
health-check.md
how-to-build.md
https.md
install-dependencies.md
mtls.md
plugin-develop.md
plugin-interceptors.md
plugins.md
profile.md
router-radixtree.md
stand-alone.md
stream-proxy.md
es/latest
zh/latest
example/apisix
kubernetes
logos
rockspec
t
utils
.asf.yaml
.gitattributes
.gitignore
.gitmodules
.luacheckrc
.markdownlint.yml
CHANGELOG.md
CODE_OF_CONDUCT.md
CODE_STYLE.md
CONTRIBUTING.md
LICENSE
Makefile
NOTICE
README.md
powered-by.md
克隆/下载
key-auth.md 4.27 KB
一键复制 编辑 原始数据 按行查看 历史
title
key-auth

Summary

Name

key-auth is an authentication plugin, it should work with consumer together.

Add Key Authentication (also sometimes referred to as an API key) to a Service or a Route. Consumers then add their key either in a querystring parameter or a header to authenticate their requests.

Attributes

For consumer side:

Name Type Requirement Default Valid Description
key string required different consumer objects should use different values, it should be unique.

For route side:

Name Type Requirement Default Valid Description
header string optional apikey the header we get the key from

How To Enable

Two steps are required:

  1. creates a consumer object, and set the attributes of plugin key-auth.
curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "username": "jack",
    "plugins": {
        "key-auth": {
            "key": "auth-one"
        }
    }
}'

You can open dashboard with a browser: http://127.0.0.1:9080/apisix/dashboard/, to complete the above operation through the web interface, first add a route:

Then add key-auth plugin:

  1. creates a route or service object, and enable plugin key-auth.
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "methods": ["GET"],
    "uri": "/index.html",
    "id": 1,
    "plugins": {
        "key-auth": {}
    },
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "39.97.63.215:80": 1
        }
    }
}'

If you don't want to fetch key from the default apikey header, you can customize the header:

{
    "key-auth": {
        "header": "Authorization"
    }
}

Test Plugin

Here is a correct test example:

$ curl http://127.0.0.2:9080/index.html -H 'apikey: auth-one' -i
HTTP/1.1 200 OK
...

If the request does not set apikey correctly, will get a 401 response.

$ curl http://127.0.0.2:9080/index.html -i
HTTP/1.1 401 Unauthorized
...
{"message":"Missing API key found in request"}

$ curl http://127.0.0.2:9080/index.html -H 'apikey: abcabcabc' -i
HTTP/1.1 401 Unauthorized
...
{"message":"Invalid API key in request"}

Disable Plugin

When you want to disable the key-auth plugin, it is very simple, you can delete the corresponding json configuration in the plugin configuration, no need to restart the service, it will take effect immediately:

$ curl http://127.0.0.1:2379/v2/keys/apisix/routes/1 -X PUT -d value='
{
    "uri": "/index.html",
    "plugins": {},
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "39.97.63.215:80": 1
        }
    }
}'

The key-auth plugin has been disabled now. It works for other plugins.

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Lua
1
https://gitee.com/liulad/apisix.git
git@gitee.com:liulad/apisix.git
liulad
apisix
apisix
master

搜索帮助

371d5123 14472233 46e8bd33 14472233