Basic Auth

功能描述

鉴权方式的一种,设置Basic Auth的Username与Password,不能通过认证的用户将无权访问接口。

OpenAPI配置鉴权及进行请求的示例

配置参数说明

参数名说明是否必填默认值值可能性
name实例名string
driver所使用的鉴权类别"basic"
description描述string
hide_credentials是否隐藏请求中鉴权密钥的字段falsebool
user用户列表object_array
user -> username用户名string
user -> password用户密码string
user -> expire过期时间 类型是unix时间戳 范围>=0 值为0表示永久有效int
user -> labels标签,object中的键值对会被均赋值到通过该密钥鉴权后的请求的上下文中,可被插件使用,例如access-log。object

返回参数说明

参数名类型是否必含说明
idstring实例id
namestring实例名
driverstring驱动名
descriptionstring描述
professionstring模块名
createstring创建时间
updatestring更新时间
hide_credentialsbool是否隐藏请求中鉴权密钥的字段
userobject_array用户列表

备注:返回体内的user参考请求配置参数,在此不再赘述。

请求中鉴权参数填写位置说明

参数名说明必填值可能性参数位置
Authorization-Type鉴权方式basic、basic_auth、basic-auth、basicauthHeader
Authorization鉴权值Basic+空格+(username:password用base64加密后的字符串)Header

全局配置

在使用basic鉴权插件之前,需要在全局插件配置中将鉴权插件状态设置为enable,具体配置点此跳转

curl -X POST  'http://127.0.0.1:9400/api/setting/plugin' -H 'Content-Type:application/json' -d '{
   "plugins":[{
      "id":"eolinker.com:apinto:auth",
      "name":"myAuth",
      "status":"enable"
   }]
}'

创建鉴权

curl -X POST  \
  'http://127.0.0.1:9400/api/auth' \
  -H 'Content-Type:application/json' \
  -d '{
	"name": "demo_basic",
	"driver": "basic",
	"description": "basic鉴权,当前仅配置了一组user",
	"user": [{
		"username": "apinto",
		"password": "123456",
		"expire": 0
	}]
}'
返回结果示例
{
	"create": "2022-06-13 18:06:34",
	"description": "basic鉴权,当前仅配置了一组user",
	"driver": "basic",
	"hide_credentials": false,
	"id": "demo_basic@auth",
	"name": "demo_basic",
	"profession": "auth",
	"update": "2022-06-13 18:06:34",
	"user": [{
		"expire": 0,
		"labels": null,
		"password": "123456",
		"username": "apinto"
	}]
}
返回的鉴权id为demo_basic@auth

创建服务

鉴权id绑定服务:将上一步生成的鉴权id添加至服务plugins配置中的auth数组

备注:匿名服务配置的是apinto官方示例接口,将返回请求的相关信息。

curl -X POST  \
  'http://127.0.0.1:9400/api/service' \
  -H 'Content-Type:application/json' \
  -d '{
	"name": "basic_service",
	"driver": "http",
	"description": "该服务使用了basic鉴权",
	"timeout": 10000,
	"retry": 3,
	"scheme": "https",
	"nodes": ["demo-apinto.eolink.com:8280"],
	"balance": "round-robin",
	"plugins": {
		"myAuth": {
			"disable": false,
			"config": {
				"auth": ["demo_basic@auth"]
			}
		}
	}
}'
返回结果示例
返回的serviceID为basic_service@service

创建路由

服务id绑定路由:上一步生成的服务id绑定至路由路由的target字段

curl -X POST  \
  'http://127.0.0.1:9400/api/router' \
  -H 'Content-Type:application/json' \
  -d '{
	"name": "basic_router",
	"driver": "http",
	"description": "创建使用鉴权basic服务的路由",
	"listen": 8099,
	"rules": [{
		"location": "/demo/basic"
	}],
	"target": "basic_service@service"
}'

请求示例

curl -X GET  \
  'http://127.0.0.1:8099/demo/basic' \
  -H 'Content-Type:application/x-www-form-urlencoded' \
  -H 'Authorization-Type:basic' \
  -H 'Authorization: Basic YXBpbnRvOjEyMzQ1Ng=='

请求返回示例

{
	"body": "",
	"header": {
		"Accept": ["*/*"],
		"Authorization": ["Basic Z29rdToxMjM0NTY="],
		"Authorization-Type": ["basic"],
		"Content-Type": ["application/x-www-form-urlencoded"],
		"User-Agent": ["curl/7.68.0"],
		"X-Forwarded-For": ["127.0.0.1,127.0.0.1"]
	},
	"host": "127.0.0.1:8099",
	"method": "GET",
	"path": "/demo/basic",
	"query": {},
	"remote_addr": "127.0.0.1:1791",
	"url": "/demo/basic"
}