【问题标题】:Keycloak: Get UserSessionModel of the current SSO sessionKeycloak:获取当前 SSO 会话的 UserSessionModel
【发布时间】:2021-01-19 11:14:54
【问题描述】:

Keycloak 11.0.2

  1. 有没有办法让UserSessionModel 分配给自定义身份验证器中的当前 SSO 会话?

我可以使用List<UserSessionModel>:

List<UserSessionModel> userSessions = context.getSession().sessions().getUserSessions(context.getRealm(), context.getUser());

但我不知道我可以使用AutheticationFlowContext 使用哪个过滤属性来过滤列表并获取当前 SSO 会话的 UserSessionModel。

现在我通过从身份验证请求 cookie KEYCLOAK_SESSION(它的最后一段)获取的 UserSessionModel.id 进行过滤。也许有一种直接的方法可以以某种方式使用AuthenticationFlowContext 来获取UserSessionModel.id

  1. 我必须使用UserSessionModel.getNote() 来检索之前在同一 SSO 的另一个身份验证流程中设置的 UserSessionNotes。

直接方法不适用于我在另一个身份验证流程中设置的UserSessionNotes(但在同一个 SSO 中):

@Override
public void authenticate(AuthenticationFlowContext context) {
    Map<String,String> sessionNotes = context.getAuthenticationSession().getUserSessionNotes();
    // sessionNotes does not reflect notes set in another Authentication flows of the same SSO
    ...

}

所以,如果有人知道另一种方式来采取UserSessionNotes w/o UserSessionModel 这也是解决方案。

【问题讨论】:

    标签: keycloak authenticator


    【解决方案1】:

    我在 Keycloak 论坛https://keycloak.discourse.group/t/getting-usersessionnotes-returns-null-while-data-persist/5172收到了答复

    在 Authenticator 中获取当前 SSO 的UserSessionModel

    @Override
    public void authenticate(AuthenticationFlowContext context) {
        UserSessionModel userSessionModel;
        AuthenticationManager.AuthResult authResult = AuthenticationManager.authenticateIdentityCookie(context.getSession(),
                context.getRealm(), true);
        if (authResult != null) {
            // That is it:
            userSessionModel = authResult.getSession();
        }
    

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多