2021-02-26 21:40:08 +08:00
---
title: key-auth
---
2019-10-31 09:27:28 +08:00
<!--
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
-->
2021-02-26 21:40:08 +08:00
## Summary
2021-01-03 11:26:40 +08:00
2019-06-06 15:59:30 +08:00
- [**Name** ](#name )
- [**Attributes** ](#attributes )
- [**How To Enable** ](#how-to-enable )
- [**Test Plugin** ](#test-plugin )
- [**Disable Plugin** ](#disable-plugin )
2019-06-05 13:25:10 +08:00
2019-06-06 15:59:30 +08:00
## Name
2019-05-23 21:13:15 +08:00
2019-06-06 15:59:30 +08:00
`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
2020-09-23 08:11:27 +08:00
| Name | Type | Requirement | Default | Valid | Description |
| ---- | ------ | ----------- | ------- | ----- | ---------------------------------------------------------------------------- |
| key | string | required | | | different consumer objects should use different values, it should be unique. |
2019-11-24 21:15:39 +08:00
2019-06-06 15:59:30 +08:00
## How To Enable
Two steps are required:
1. creates a consumer object, and set the attributes of plugin `key-auth` .
2019-06-19 19:14:58 +08:00
```shell
2020-11-28 19:05:14 +08:00
curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
2019-06-19 19:14:58 +08:00
{
"username": "jack",
2019-09-03 13:53:41 +08:00
"plugins": {
"key-auth": {
2019-09-22 06:05:58 +08:00
"key": "auth-one"
2019-09-03 13:53:41 +08:00
}
}
2019-06-19 19:14:58 +08:00
}'
```
2019-05-23 21:13:15 +08:00
2019-09-23 06:13:06 +08:00
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:
2021-02-25 16:32:21 +08:00
![](../../../assets/images/plugin/key-auth-1.png)
2019-09-23 06:13:06 +08:00
Then add key-auth plugin:
2021-02-25 16:32:21 +08:00
![](../../../assets/images/plugin/key-auth-2.png)
2019-09-23 06:13:06 +08:00
2019-06-06 15:59:30 +08:00
2. creates a route or service object, and enable plugin `key-auth` .
2019-06-19 19:14:58 +08:00
```shell
2020-11-28 19:05:14 +08:00
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
2019-06-19 19:14:58 +08:00
{
2019-09-03 13:53:41 +08:00
"methods": ["GET"],
"uri": "/index.html",
"id": 1,
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"39.97.63.215:80": 1
}
}
2019-06-19 19:14:58 +08:00
}'
```
2019-06-06 15:59:30 +08:00
## Test Plugin
Here is a correct test example:
```shell
2019-12-08 10:20:46 +08:00
$ curl http://127.0.0.2:9080/index.html -H 'apikey: auth-one' -i
2019-06-06 15:59:30 +08:00
HTTP/1.1 200 OK
...
2019-05-23 21:13:15 +08:00
```
2019-06-06 15:59:30 +08:00
If the request does not set `apikey` correctly, will get a `401` response.
```shell
$ 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
2019-09-03 13:53:41 +08:00
When you want to disable the `key-auth` plugin, it is very simple,
2019-06-06 15:59:30 +08:00
you can delete the corresponding json configuration in the plugin configuration,
no need to restart the service, it will take effect immediately:
```shell
2019-06-06 16:27:09 +08:00
$ curl http://127.0.0.1:2379/v2/keys/apisix/routes/1 -X PUT -d value='
2019-06-06 15:59:30 +08:00
{
2019-09-03 13:53:41 +08:00
"uri": "/index.html",
"plugins": {},
"upstream": {
"type": "roundrobin",
"nodes": {
"39.97.63.215:80": 1
}
}
2019-06-06 15:59:30 +08:00
}'
```
2019-09-03 13:53:41 +08:00
The `key-auth` plugin has been disabled now. It works for other plugins.