Appearance
身份认证
身份认证分类的插件可为APP中的服务提供访问的安全认证,防止未经授权的访问。
安全认证插件之间是互斥的,不可为APP安装不同的安全认证插件
basic_auth
功能说明
该插件用于实现HTTP的Basic Auth认证,认证的用户名和密码在消费者管理中设置。
示例
插件安装
在(主菜单)APP管理
-APP发布
界面中,对已发布的demo项目点击插件管理
。

点击身份认证中的basic_auth插件下方的安装
按钮,即可为APP安装该插件。

消费者配置
消费者用户名密码设置
在(主菜单)APP管理
-消费者管理
界面中,对已有消费者consumer1
的配置进行编辑,修改用户名、密码为admin
/admin
。

消费者授权
在(主菜单)APP管理
-APP授权
界面中,选中demo应用,点击右方授权列表的授权
按钮。

选中授权消费者consumer1
后,勾选APP内需授权的接口a01
后确认即完成授权。

访问服务
当不用Basic Auth 认证访问a01接口时,访问不成功:
使用curl请求接口:
$ curl http://localhost:6636/demo/a01/helloWorld
$ curl http://localhost:6636/demo/a01/helloWorld
接口返回:
basic_auth failed, exits with http status code: 401. cause: invalid authorization, username does not exist, missing related consumer
basic_auth failed, exits with http status code: 401. cause: invalid authorization, username does not exist, missing related consumer
使用消费者consumer1的用户名和密码作为Basic Auth 认证访问a01接口时,访问成功:
使用curl请求接口:
$ curl -u admin:admin -X GET http://localhost:6636/demo/a01/helloWorld
$ curl -u admin:admin -X GET http://localhost:6636/demo/a01/helloWorld
接口返回:
json
{
"data": "hello world",
"resCode": "0",
"resMsg": "success"
}
{
"data": "hello world",
"resCode": "0",
"resMsg": "success"
}
key_auth
功能说明
用于实现访问令牌认证。在插件配置中设定访问时的认证参数名,以消费者的访问令牌属性值作为认证令牌值。
消费者的访问令牌从消费者管理中设置。
插件配置
在插件配置中可配置从哪里认证token值,分别可配置从header参数或url查询参数中认证token值。

示例
插件安装
在(主菜单)APP管理
-APP发布
界面中,对已发布的demo项目点击插件管理。

点击身份认证中的key_auth插件下方的安装
按钮,即可为APP安装该插件。

消费者配置
消费者访问令牌
在(主菜单)APP管理
-消费者管理
界面中,对已有消费者consumer1
的配置进行编辑,点击访问令牌右方的生成
按钮为该消费者生成访问令牌。

消费者授权
在(主菜单)APP管理
-APP授权
界面中,选中demo应用,点击授权列表右上方的授权
按钮。

选中授权消费者consumer1
后,勾选APP内需授权的接口a01
后确认即完成授权。

访问服务
当不带token访问a01接口时,访问不成功:
使用curl请求接口:
$ curl http://localhost:6636/demo/a01/helloWorld
$ curl http://localhost:6636/demo/a01/helloWorld
接口返回:
key_auth failed, exits with http status code: 401. cause: missing token found in request
key_auth failed, exits with http status code: 401. cause: missing token found in request
使用消费者consumer1的token访问a01接口时,访问成功:
使用curl请求接口:
$ curl -H "token: gwTlW1UDQ5kL2blncb" "http://localhost:6636/demo/a01/helloWorld"
$ curl -H "token: gwTlW1UDQ5kL2blncb" "http://localhost:6636/demo/a01/helloWorld"
接口返回:
json
{
"data": "hello world",
"resCode": "0",
"resMsg": "success"
}
{
"data": "hello world",
"resCode": "0",
"resMsg": "success"
}