如何从当前会话获取用户角色



使用 Keycloak 登录我的系统后,我想从我登录的用户那里获取用户角色映射(在 keycloak 管理控制台上定义)。我能够获取名字、姓氏、Id、token-id,但是当尝试获取角色时,我得到一个空数组:

private List<KeycloakOidcProfile> getUserData() {
    final PlayWebContext context = new PlayWebContext(ctx(), playSessionStore);
    final ProfileManager<KeycloakOidcProfile> profileManager = new ProfileManager(context);

    System.out.println("Roles->>>"+ profileManager.get(true).get().getRoles()); //here i get -> []
    System.out.println("FirstName->>>"+ profileManager.get(true).get().getFirstName());
    System.out.println("Last Name->>>"+ profileManager.get(true).get().getFamilyName());
    System.out.println("ID->>>"+ profileManager.get(true).get().getId());

    return profileManager.getAll(true);

我相信,您应该实现自己的 AuthorizationGenerator 并将其附加到客户端(在本例中为 Keycloak 客户端),以便将令牌角色映射到KeycloakOidcProfile,但请确保 OIP 在其令牌中发送角色。

有一些胶水 http://www.pac4j.org/docs/clients.html

最新更新