簡體   English   中英

如何從當前會話中獲取用戶角色?

[英]How can I get user roles from current session?

使用 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);

我相信,您應該實現自己的AuthorizationGenerator並將其附加到客戶端(在本例中為 Keycloak 客戶端),以便將令牌角色映射到KeycloakOidcProfile ,但請確保OIP在其令牌中發送角色。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM