wolf-rbac

    属性

    插件会增加这些接口:

    • /apisix/plugin/wolf-rbac/login
    • /apisix/plugin/wolf-rbac/change_pwd
    • /apisix/plugin/wolf-rbac/user_info

    你可能需要通过 interceptors 来保护它们。

    依赖项

    添加应用, 管理员, 普通用户, 权限, 资源 及给用户授权.

    1. 创建一个 consumer 对象,并设置插件 wolf-rbac 的值。

    你可以使用浏览器打开 dashboard:http://127.0.0.1:9080/apisix/dashboard/,通过 web 界面来完成上面的操作,先增加一个 consumer:

    注意: 上面填写的 appid 需要在 wolf 控制台中已经存在的.

    1. 创建 Route 或 Service 对象,并开启 wolf-rbac 插件。
    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "methods": ["GET"],
    4. "uri": "/*",
    5. "plugins": {
    6. "wolf-rbac": {}
    7. },
    8. "upstream": {
    9. "type": "roundrobin",
    10. "nodes": {
    11. "www.baidu.com:80": 1
    12. }
    13. }
    14. }'

    测试插件

    首先进行登录获取 wolf-rbac token:

    下面的 appid, username, password 必须为 wolf 系统中真实存在的.

    • 以 POST application/json 方式登陆.
    1. curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/login -i \
    2. -d '{"appid": "restful", "username":"test", "password":"user-password"}'
    3. Date: Wed, 24 Jul 2019 10:33:31 GMT
    4. Content-Type: text/plain
    5. Transfer-Encoding: chunked
    6. Connection: keep-alive
    7. Server: APISIX web server
    8. {"rbac_token":"V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts","user_info":{"nickname":"test","username":"test","id":"749"}}
    • 以 POST x-www-form-urlencoded 方式登陆
    1. curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/login -i \
    2. -H "Content-Type: application/x-www-form-urlencoded" \
    3. -d 'appid=restful&username=test&password=user-password'

    使用获取到的 token 进行请求尝试

    • 缺少 token
    • token 放到请求头(Authorization)中:
    1. curl http://127.0.0.1:9080/ -H"Host: www.baidu.com" \
    2. -H 'Authorization: V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -i
    3. HTTP/1.1 200 OK
    4. <!DOCTYPE html>
    • token 放到请求头(x-rbac-token)中:
    1. curl http://127.0.0.1:9080/ -H"Host: www.baidu.com" \
    2. -H 'x-rbac-token: V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -i
    3. HTTP/1.1 200 OK
    4. <!DOCTYPE html>
    • token 放到请求参数中:
    1. curl 'http://127.0.0.1:9080?rbac_token=V1%23restful%23eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -H"Host: www.baidu.com" -i
    2. HTTP/1.1 200 OK
    3. <!DOCTYPE html>
    • token 放到 cookie 中:

    获取 RBAC 用户信息

    1. curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/user_info \
    2. --cookie x-rbac-token=V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts -i
    3. HTTP/1.1 200 OK
    4. "user_info":{
    5. "nickname":"test",
    6. "lastLogin":1582816780,
    7. "id":749,
    8. "username":"test",
    9. "appIDs":["restful"],
    10. "manager":"none",
    11. "permissions":{"USER_LIST":true},
    12. "profile":null,
    13. "roles":{},
    14. "createTime":1578820506,
    15. "email":""
    16. }
    17. }

    修改 RBAC 用户密码

    1. curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/change_pwd \
    2. -H "Content-Type: application/json" \
    3. --cookie x-rbac-token=V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts -i \
    4. -X PUT -d '{"oldPassword": "old password", "newPassword": "new password"}'
    5. HTTP/1.1 200 OK
    6. {"message":"success to change password"}
    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "methods": ["GET"],
    4. "uri": "/*",
    5. "plugins": {
    6. },
    7. "upstream": {
    8. "type": "roundrobin",
    9. "nodes": {
    10. "www.baidu.com:80": 1
    11. }
    12. }