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

时间:2019-01-31 11:31:27

标签: java roles keycloak pac4j

使用Keycloak登录系统后,我想从我登录的用户那里获取用户角色映射(在keycloak管理控制台上定义)。 我可以获取名字,姓氏,ID,令牌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);

1 个答案:

答案 0 :(得分:0)

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

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