Keycloak检索自定义属性KeycloakPrincipal [英] Keycloak retrieve custom attributes to KeycloakPrincipal

查看:2217
本文介绍了Keycloak检索自定义属性KeycloakPrincipal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我休息的服务,我可以使用获得认证后的主要信息

In my rest service i can obtain the principal information after authentication using

KeycloakPrincipal kcPrincipal = (KeycloakPrincipal) servletRequest.getUserPrincipal();

语句。

Keycloak校长一点儿也不cotain我需要有关身份验证的用户的所有信息。
它是不可能性定制我自己的主要类型?
在keycloak - 服务器端我已经开发了一个用户联合提供。我看到的usermodel使得有可能为一组自定义属性添加到我的用户。

Keycloak principal does'nt cotain all the information i need about the authenticated user. Is it possibile to customize my own principal type? On the keycloak-server-end i ve developed a user federation provider. I saw that UserModel makes possible to add a set of custom attributes to my user.

难道不可能性插入在code我的自定义主?

Is it possibile to insert my custom principal in that code?

是否有可能检索keycloak校长这个属性?

Is it possible to retrieve this attributes from keycloak principal?

什么是路?

推荐答案

要添加自定义属性,你需要做三件事情:

To add custom attributes you need to do three things:


  1. 属性添加到管理控制台

  2. 添加声明映射

  3. 访问要求

第一种解释pretty这里好:<一href=\"http://docs.jboss.org/keycloak/docs/1.2.0.Beta1/userguide/html/custom-user-attributes.html\">http://docs.jboss.org/keycloak/docs/1.2.0.Beta1/userguide/html/custom-user-attributes.html

The first one is explained pretty good here: http://docs.jboss.org/keycloak/docs/1.2.0.Beta1/userguide/html/custom-user-attributes.html

添加声明映射:


  1. 打开您的领域的管理控制台。

  2. 转到客户端,打开客户端

  3. 这只能用于设置>访问类型的机密或公众(不记名只)

  4. 转到映射器

  5. 从您的属性到JSON
  6. 创建一个映射
  7. 选中添加到ID令牌

  1. Open the admin console of your realm.
  2. Go to Clients and open your client
  3. This only works for Settings > Access Type confidential or public (not bearer-only)
  4. Go to Mappers
  5. Create a mapping from your attribute to json
  6. Check "Add to ID token"

访问声明:

最后主要userPrincipal = HTT prequest.getUserPrincipal();

final Principal userPrincipal = httpRequest.getUserPrincipal();

if (userPrincipal instanceof KeycloakPrincipal) {

    KeycloakPrincipal<KeycloakSecurityContext> kp = (KeycloakPrincipal<KeycloakSecurityContext>) userPrincipal;
    IDToken token = kp.getKeycloakSecurityContext().getIdToken();

    Map<String, Object> otherClaims = token.getOtherClaims();

    if (otherClaims.containsKey("YOUR_CLAIM_KEY")) {
        yourClaim = String.valueOf(otherClaims.get("YOUR_CLAIM_KEY"));
    }
} else {
    throw new RuntimeException(...);
}

希望这有助于和适合您的使用案例。我用这个,因为我添加了自定义主题自定义属性。

Hope this helps and fits your use case. I used this for a custom attribute I added with a custom theme.

这篇关于Keycloak检索自定义属性KeycloakPrincipal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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