WSO2 APIM - 在 JWT 有效负载中添加用户角色 [英] WSO2 APIM - Add user roles in JWT payload

查看:16
本文介绍了WSO2 APIM - 在 JWT 有效负载中添加用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些通过 WSO2 APIM 公开 REST 的 SpringBoot 微服务.

微服务本身不实现任何类型的身份验证或授权机制,它被委托给 APIM.

如果我按照描述将 API 设置为使用密码授予

但生成的 JWT 令牌不包含任何有关角色的信息.这是一个示例令牌:

<预> <代码> eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5UZG1aak00WkRrM05qWTBZemM1TW1abU9EZ3dNVEUzTVdZd05ERTVNV1JsWkRnNE56YzRaQT09In0.eyJodHRwOlwvXC93c28yLm9yZ1wvY2xhaW1zXC9hcHBsaWNhdGlvbnRpZXIiOiJVbmxpbWl0ZWQiLCJodHRwOlwvXC93c28yLm9yZ1wvY2xhaW1zXC92ZXJzaW9uIjoidjEiLCJodHRwOlwvXC93c28yLm9yZ1wvY2xhaW1zXC9rZXl0eXBlIjoiUFJPRFVDVElPTiIsImlzcyI6IndzbzIub3JnXC9wcm9kdWN0c1wvYW0iLCJodHRwOlwvXC93c28yLm9yZ1wvY2xhaW1zXC9hcHBsaWNhdGlvbm5hbWUiOiJDYWRhc3RybyBkZSBDbGllbnRlcyIsImtleXR5cGUiOiJTQU5EQk9YIiwiaHR0cDpcL1wvd3NvMi5vcmdcL2NsYWltc1wvZW5kdXNlciI6ImVtaWxpb0BjYXJib24uc3VwZXIiLCJodHRwOlwvXC93c28yLm9yZ1wvY2xhaW1zXC9lbmR1c2VyVGVuYW50SWQiOiItMTIzNCIsImh0dHA6XC9cL3dzbzIub3JnXC9jbGFpbXNcL3N1YnNjcmliZXIiOiJhZG1pbiIsImh0dHA6XC9cL3dzbzIub3JnXC9jbGFpbXNcL3RpZXIiOiJVbmxpbWl0ZWQiLCJzY29wZSI6ImRlZmF1bHQiLCJleHAiOiIxNTk5NTYyOTQ4MDI4IiwiaHR0cDpcL1wvd3NvMi5vcmdcL2NsYWltc1wvYXBwbGljYXRpb25pZCI6IjIiLCJodHRwOlwvXC93c28yLm9yZ1wvY2xhaW1zXC91c2VydHlwZSI6IkFwcGxpY2F0aW9uX1VzZXIiLCJjb25zdW1lcktleSI6IktJaTdnUk1RYmg1OWZGbmpVOFhNbnhGcm9pNGEiLCJodHRwOlwvXC93c28yLm9yZ1wvY2xhaW1zXC9hcGljb250ZXh0IjoiXC9ia25nXC92MSJ9.km4w2V7dGmoGl8f4_ZqKHvdofAPLOOw__GPjWKrpjYelbi7IjDIpRODEZNn8hE1krRdDTSjKRviJ-NBvXtTXIiLdfPh1p-zNtX26vrS77ZcSZ2WsQA7Ku21YMqcm6cyZvEhZ99qfTxOtbJfkwt6Yt8itkyr-aqk83pNp85LTnwtNboib9VOOvh37zNEJUImzKw4WvENp4SGLuHO978FriHyHPN9vibzPjpItW5DOXTFNdN4rP6RK_vcOH6hpuZHwivJpTHxf9qMB3Gd2yTig-HKR-sZGbx89pQf8kqtCLWbhRG5jOtcEJNf2CSNLB0Glg_e4F6LfhVD5JUCz15jdlg

当我在

  • 使用 scope=openid 参数发送令牌请求

    curl -k -X POST https://localhost:8243/token -d "grant_type=password&username=<Username>&password=<Password>&scope=openid"-H 授权:基本 <凭据>"

  • 响应访问令牌将包含这种格式的角色

    {子":admin@carbon.super","iss": "https://localhost:9443/oauth2/token",组":[内部/订户",内部/创作者",应用程序/apim_devportal",应用程序/admin_NewApp_PRODUCTION",内部/出版商",内部/每个人",内部/分析",],...}

  • I'm developing some SpringBoot microservices that exposes REST through WSO2 APIM.

    Microservice itself does not implement any kind of authentication or authorization mecanism, it is delegated to APIM.

    If I set API to use Password Grant as described here, front end application can authenticate and generate JWT token.

    The problem now is that I can't fetch user roles from JWT payload because it is not being added by APIM. This information is important because front-end render menus and buttons based on user roles.

    The user I'm passing when generate token does have some roles as you can see bellow:

    But generated JWT token does not include any information about roles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C9ia25nXC92MSJ9.km4w2V7dGmoGl8f4_ZqKHvdofAPLOOw__GPjWKrpjYelbi7IjDIpRODEZNn8hE1krRdDTSjKRviJ-NBvXtTXIiLdfPh1p-zNtX26vrS77ZcSZ2WsQA7Ku21YMqcm6cyZvEhZ99qfTxOtbJfkwt6Yt8itkyr-aqk83pNp85LTnwtNboib9VOOvh37zNEJUImzKw4WvENp4SGLuHO978FriHyHPN9vibzPjpItW5DOXTFNdN4rP6RK_vcOH6hpuZHwivJpTHxf9qMB3Gd2yTig-Hkr-sZGbx89pQf8kqtCLWbhRG5jOtcEJNf2CSNLB0Glg_e4F6LfhVD5JUCz15jdlg
    

    When I extract it in https://jwt.io/ I get following payload:

    {
      "http://wso2.org/claims/applicationtier": "Unlimited",
      "http://wso2.org/claims/version": "v1",
      "http://wso2.org/claims/keytype": "PRODUCTION",
      "iss": "wso2.org/products/am",
      "http://wso2.org/claims/applicationname": "Cadastro de Clientes",
      "keytype": "SANDBOX",
      "http://wso2.org/claims/enduser": "emilio@carbon.super",
      "http://wso2.org/claims/enduserTenantId": "-1234",
      "http://wso2.org/claims/subscriber": "admin",
      "http://wso2.org/claims/tier": "Unlimited",
      "scope": "default",
      "exp": "1599562948028",
      "http://wso2.org/claims/applicationid": "2",
      "http://wso2.org/claims/usertype": "Application_User",
      "consumerKey": "KIi7gRMQbh59fFnjU8XMnxFroi4a",
      "http://wso2.org/claims/apicontext": "/bkng/v1"
    }
    

    How do I add user roles to JWT payload? Do I need to implement a custom generator as described here?

    Thanks in advance!

    解决方案

    Easiest way to get role claim included in the auth JWT is to add a claim mapping in service provider level and request the token with openid scopes. To do this try below steps.

    1. Log in to management console https://<host>:<port>/carbon

    2. List service providers in the left menu

    3. Go to edit on the required service provider (Each application in the developer portal has a mapping service provider)

    4. Add a claim mapping to role claim as below

    5. Send the token request with the scope=openid parameter

      curl -k -X POST https://localhost:8243/token -d "grant_type=password&username=<Username>&password=<Password>&scope=openid" -H "Authorization: Basic <Credentials>"
      

    6. Response access token will contain roles in this format

      {
          "sub": "admin@carbon.super",
          "iss": "https://localhost:9443/oauth2/token",
          "groups": [
              "Internal/subscriber",
              "Internal/creator",
              "Application/apim_devportal",
              "Application/admin_NewApp_PRODUCTION",
              "Internal/publisher",
              "Internal/everyone",
              "Internal/analytics",
          ],
          ...
      }
      

    这篇关于WSO2 APIM - 在 JWT 有效负载中添加用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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