2021-02-26 21:40:08 +08:00
---
title: basic-auth
---
2020-01-13 17:43:04 +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
2020-03-22 19:22:23 +08:00
2020-01-13 17:43:04 +08:00
- [**Name** ](#name )
- [**Attributes** ](#attributes )
- [**How To Enable** ](#how-to-enable )
- [**Test Plugin** ](#test-plugin )
- [**Disable Plugin** ](#disable-plugin )
## Name
`basic-auth` is an authentication plugin that need to work with `consumer` . Add Basic Authentication to a `service` or `route` .
The `consumer` then adds its key to the request header to verify its request.
2020-10-14 08:32:51 +08:00
For more information on Basic authentication, refer to [Wiki ](https://en.wikipedia.org/wiki/Basic_access_authentication ) for more information.
2020-01-13 17:43:04 +08:00
## Attributes
2020-09-23 08:11:27 +08:00
| Name | Type | Requirement | Default | Valid | Description |
| -------- | ------ | ----------- | ------- | ----- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| username | string | required | | | Different `consumer` should have different value which is unique. When different `consumer` use a same `username` , a request matching exception would be raised. |
| password | string | required | | | the user's password |
2020-01-13 17:43:04 +08:00
## How To Enable
2020-03-22 19:22:23 +08:00
### 1. set a consumer and config the value of the `basic-auth` option
2020-01-13 17:43:04 +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 '
2020-01-13 17:43:04 +08:00
{
"username": "foo",
"plugins": {
"basic-auth": {
"username": "foo",
"password": "bar"
}
}
}'
```
you can visit Dashboard `http://127.0.0.1:9080/apisix/dashboard/` and add a Consumer through the web console:
2021-02-25 16:32:21 +08:00
![auth-1 ](../../../assets/images/plugin/basic-auth-1.png )
2020-01-13 17:43:04 +08:00
then add basic-auth plugin in the Consumer page:
2021-02-25 16:32:21 +08:00
![auth-2 ](../../../assets/images/plugin/basic-auth-2.png )
2020-01-13 17:43:04 +08:00
2020-10-29 18:53:23 +08:00
### 2. add a Route or add a Service, and enable the `basic-auth` plugin
2020-01-13 17:43:04 +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 '
2020-01-13 17:43:04 +08:00
{
"methods": ["GET"],
"uri": "/hello",
"plugins": {
"basic-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'
```
## Test Plugin
2020-03-22 19:22:23 +08:00
- missing Authorization header
2020-01-13 17:43:04 +08:00
```shell
2020-09-23 08:11:27 +08:00
$ curl -i http://127.0.0.1:9080/hello
2020-01-13 17:43:04 +08:00
HTTP/1.1 401 Unauthorized
...
{"message":"Missing authorization in request"}
```
2020-03-22 19:22:23 +08:00
- user is not exists:
2020-01-13 17:43:04 +08:00
```shell
$ curl -i -ubar:bar http://127.0.0.1:9080/hello
HTTP/1.1 401 Unauthorized
...
{"message":"Invalid user key in authorization"}
```
2020-03-22 19:22:23 +08:00
- password is invalid:
2020-01-13 17:43:04 +08:00
```shell
$ curl -i -ufoo:foo http://127.0.0.1:9080/hello
HTTP/1.1 401 Unauthorized
...
{"message":"Password is error"}
```
2020-03-22 19:22:23 +08:00
- success:
2020-01-13 17:43:04 +08:00
```shell
$ curl -i -ufoo:bar http://127.0.0.1:9080/hello
HTTP/1.1 200 OK
...
hello, world
```
## Disable Plugin
When you want to disable the `basic-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:
```shell
$ curl http://127.0.0.1:2379/apisix/admin/routes/1 -X PUT -d value='
{
"methods": ["GET"],
"uri": "/hello",
"plugins": {},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'
```