【发布时间】:2019-06-24 20:36:48
【问题描述】:
使用 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);
【问题讨论】:
-
我是 Keycloak 的新手,但您不需要将角色请求为特殊范围吗?
-
@GabrielPatrício,发现与 Node Js 相同的问题。看看这个逻辑可能会对你有所帮助stackoverflow.com/questions/40285305/…stackoverflow.com/questions/29014894/…
-
@JeroenSteenbeeke 你能详细说明一下吗?这也是我第一次使用 Keycloak...
-
@AmanJaiswal 我已经检查了第二个链接,我的问题是我正在处理会话而不是请求,所以我无法初始化 KeycloakPrincipal
-
@JeroenSteenbeeke 我发现了问题所在。在 keycloak 管理控制台中,您必须定义要包含在用户信息中的客户端范围(角色),这样您就可以通过定义的属性名称进行访问。非常感谢!