Skip to content

身份认证

身份认证分类的插件可为APP中的服务提供访问的安全认证,防止未经授权的访问。

安全认证插件之间是互斥的,不可为APP安装不同的安全认证插件

basic_auth

功能说明

该插件用于实现HTTP的Basic Auth认证,认证的用户名和密码在消费者管理中设置。

示例

插件安装

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

identAuth_img_2.png

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

identAuth_img_3.png

消费者配置

消费者用户名密码设置

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

identAuth_img_4.png

消费者授权

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

identAuth_img_5.png

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

identAuth_img_6.png

访问服务

当不用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安装该插件。

identAuth_img_3.png

消费者配置

消费者访问令牌

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

identAuth_img_4.png

消费者授权

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

identAuth_img_5.png

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

identAuth_img_6.png

访问服务

当不带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"
}