我可以在JWT令牌中保存自定义信息吗?

发布于 2025-02-12 16:07:55 字数 1569 浏览 0 评论 0原文

我想知道是否有一种方法可以在我的JWT令牌中存储“自定义”信息。

现在,我有一个在KeyCloak中创建的领域,然后当我针对KeyCloak登录时,我会得到我的Access_Token。如果我解码我的access_token,(来自jwt.io),我会获取以下信息:

{
  "exp": 1656952366,
  "iat": 1656916366,
  "jti": "be89285a-dce9-4860-9849-396044b34578",
  "iss": "https://dev.domain.com/auth/realms/miREALM",
  "aud": "account",
  "sub": "95fa9b26-0fc8-48ac-820e-71cb70183c6b",
  "typ": "Bearer",
  "azp": "MyProject",
  "session_state": "ceb61406-as9c-47f4-1461-43a63ebf939d",
  "acr": "1",
  "realm_access": {
    "roles": [
      "default-roles-myproduct-myproject",
      "offline_access",
      "uma_authorization"
    ]
  },
  "resource_access": {
    "MyProject": {
      "roles": [
        "admin"
      ]
    },
    "account": {
      "roles": [
        "manage-account",
        "manage-account-links",
        "view-profile"
      ]
    }
  },
  "scope": "openid mapPermissions profile email",
  "realm-management": {},
  "email_verified": false,
  "security-admin-console": {},
  "MyPRoject": {},
  "preferred_username": "[email protected]",
  "account-console": {},
  "given_name": "Mark",
  "broker": {},
  "name": "Mark",
  "admin-cli": {},
  "family_name": "Mark",
  "email": "[email protected]",
  "account": {}
}

我想知道是否有任何使用自定义信息创建一些节点。 例如,我希望能够保存用户寿命的地址,因此我不必访问我的数据库来检索它。

I would like to know if there is a way to store "custom" information in my JWT token.

Right now I have a REALM created in Keycloak, then when I login against Keycloak, I get my access_token. If I decode my access_token, (from jwt.io) I obtain information like:

{
  "exp": 1656952366,
  "iat": 1656916366,
  "jti": "be89285a-dce9-4860-9849-396044b34578",
  "iss": "https://dev.domain.com/auth/realms/miREALM",
  "aud": "account",
  "sub": "95fa9b26-0fc8-48ac-820e-71cb70183c6b",
  "typ": "Bearer",
  "azp": "MyProject",
  "session_state": "ceb61406-as9c-47f4-1461-43a63ebf939d",
  "acr": "1",
  "realm_access": {
    "roles": [
      "default-roles-myproduct-myproject",
      "offline_access",
      "uma_authorization"
    ]
  },
  "resource_access": {
    "MyProject": {
      "roles": [
        "admin"
      ]
    },
    "account": {
      "roles": [
        "manage-account",
        "manage-account-links",
        "view-profile"
      ]
    }
  },
  "scope": "openid mapPermissions profile email",
  "realm-management": {},
  "email_verified": false,
  "security-admin-console": {},
  "MyPRoject": {},
  "preferred_username": "[email protected]",
  "account-console": {},
  "given_name": "Mark",
  "broker": {},
  "name": "Mark",
  "admin-cli": {},
  "family_name": "Mark",
  "email": "[email protected]",
  "account": {}
}

I would like to know if there is any way to create some node with custom information.
For example, I would like to be able to save the address where the user lives, so I don't have to access my database to retrieve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2025-02-19 16:07:55

您可以将映射器添加到客户端/客户端范围。
您可以使用不同的类型。在您的情况下,“用户属性”将是最佳选择。

You can add a mapper to the client/client scope.
There are different types that you can use. In your case "User Attribute" will be the best choise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文