在 wso2 APIM 中以编程方式添加范围 [英] adding scopes programmatically in wso2 APIM

查看:21
本文介绍了在 wso2 APIM 中以编程方式添加范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 WSO2 APIM 中以编程方式创建作用域吗?我有一个要求,用户可以通过 UI 创建新角色并将一些权限与新角色关联.用户不会使用 WSO2 Web 界面;相反,他将使用内部网络应用程序为此,我必须以编程方式创建范围并将 API 与其关联.还可以手动将范围映射到角色.

can I create scopes programmatically in WSO2 APIM? I have a requirement where user can create new roles via UI and associate some permissions with the new role..User will not use WSO2 web interface ; rather he will use the inhouse web application For this, I have to programmatically create Scopes and associate API's with it. Also manually map scopes to roles.

如何以编程方式通过 WSO2 APIM 创建范围?以编程方式使用范围可能进行的所有操作是什么?如果不可能,我如何通过 WSO2 处理此类要求?

How can I create scopes via WSO2 APIM Programmatically? What all the operations possible with scopes programmatically? If it's not possible, how can I handle such requirements via WSO2?

推荐答案

您可以使用 发布者 REST API 用于此.

You can use Publisher REST APIs for this.

首先,您需要获取 API 的 swagger 定义.

First, you need to get the swagger definition of the API.

curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" 
https://127.0.0.1:9443/api/am/publisher/v0.10/apis/890a4f4d-09eb-4877-a323-57f6ce2ed79b/swagger 

你的招摇会是这样的.

{
   "swagger":"2.0",
   "paths":{
      "/menu":{
     "get":{
        "x-auth-type":"Application & Application User",
        "x-throttling-tier":"Unlimited",
        "description":"Return a list of available menu items",
        "parameters":[

        ],
        "responses":{
           "200":{
              "headers":{

              },
              "schema":{
                 "title":"Menu",
                 "properties":{
                    "list":{
                       "items":{
                          "$ref":"#/definitions/MenuItem"
                       },
                       "type":"array"
                    }
                 },
                 "type":"object"
              },
              "description":"OK."
           }
        }
     }
      }
   },
   "schemes":[
      "https"
   ],
   "produces":[
      "application/json"
   ],
   "definitions":{
      "MenuItem":{
          "title":"Pizza menu Item",
          "properties":{
              "price":{
                  "type":"string"
               },
               "description":{
               "type":"string"
               },
               "name":{
                    "type":"string"
               },
               "image":{
                    "type":"string"
                }
           },
           "required":[
              "name"
           ]
      }
   },
   "consumes":[
      "application/json"
   ],
   "info":{
      "title":"PizzaShackAPI",
      "description":"This document describe a RESTFul API for Pizza Shack online pizza delivery store.\n",
      "license":{
     "name":"Apache 2.0",
     "url":"http://www.apache.org/licenses/LICENSE-2.0.html"
      },
      "contact":{
     "email":"architecture@pizzashack.com",
     "name":"John Doe",
     "url":"http://www.pizzashack.com"
      },
      "version":"1.0.0"
   }
}

现在您可以通过更新您获得的 swagger 文件来添加新范围并将其附加到 API 资源.

Now you can add a new scope and attach it to a resource of the API by updating the swagger file you got.

像这样添加了一个新范围.

A new scope is added like this.

"x-wso2-security":{
   "apim":{
      "x-wso2-scopes":[
         {
           "description":"New scope",
           "name":"new_scope",
           "roles":"admin",
           "key":"new_scope"
         }
      ]
   }
}

它可以像这样附加到现有资源.

It can be attached to an existing resource like this.

"x-scope":"new_scope"

那么完整的 swagger 将如下所示.

Then the complete swagger will look like this.

{
   "swagger":"2.0",
   "x-wso2-security":{
      "apim":{
     "x-wso2-scopes":[
        {
           "description":"New scope",
           "name":"new_scope",
           "roles":"admin",
           "key":"new_scope"
        }
     ]
      }
   },
   "paths":{
      "/menu":{
     "get":{
        "x-auth-type":"Application & Application User",
        "x-throttling-tier":"Unlimited",
        "x-scope":"new_scope",
        "description":"Return a list of available menu items",
        "parameters":[

        ],
        "responses":{
           "200":{
              "headers":{

              },
              "schema":{
                 "title":"Menu",
                 "properties":{
                    "list":{
                       "items":{
                          "$ref":"#/definitions/MenuItem"
                       },
                       "type":"array"
                    }
                 },
                 "type":"object"
              },
              "description":"OK."
           }
        }
     }
      }
   },
   "schemes":[
      "https"
   ],
   "produces":[
      "application/json"
   ],
   "definitions":{
      "MenuItem":{
     "title":"Pizza menu Item",
     "properties":{
        "price":{
           "type":"string"
        },
        "description":{
           "type":"string"
        },
        "name":{
           "type":"string"
        },
        "image":{
           "type":"string"
        }
     },
     "required":[
        "name"
     ]
      }
   },
   "consumes":[
      "application/json"
   ],
   "info":{
      "title":"PizzaShackAPI",
      "description":"This document describe a RESTFul API for Pizza Shack online pizza delivery store.\n",
      "license":{
     "name":"Apache 2.0",
     "url":"http://www.apache.org/licenses/LICENSE-2.0.html"
      },
      "contact":{
     "email":"architecture@pizzashack.com",
     "name":"John Doe",
     "url":"http://www.pizzashack.com"
      },
      "version":"1.0.0"
   }
}

如果你在一个名为swagger.json"的文件中有这个 swagger,你可以像这样更新你的 API 的 swagger.

If you have this swagger in a file named 'swagger.json', you can update the swagger of your API like this.

curl -k -H "Authorization: Bearer b7108a70-3537-34f1-acbb-1c53b99d64dc" 
-F "apiDefinition=@swagger.json;filename=swagger.json" -X PUT https://127.0.0.1:9443/api/am/publisher/v0.10/apis/2c5f05b2-0277-42b2-92c5-862750563661/swagger

这将使用新的范围更新您的 API.

This will update your API with new scope.

这篇关于在 wso2 APIM 中以编程方式添加范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆