密钥泄漏:获取当前SSO会话的UserSessionModel

问题描述

Keycloak 11.0.2

  1. 是否可以通过自定义身份验证器将UserSessionModel分配给当前SSO会话?

我可以参加List<UserSessionModel>

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

但是我不知道我可以使用AutheticationFlowContext来对哪个过滤属性进行过滤,并采用当前SSO会话的UserSessionModel。

现在,我正在过滤从身份验证请求Cookie UserSessionModel.id(其最后一部分)中提取KEYCLOAK_SESSION。也许有直接方法以某种方式使用UserSessionModel.id来使用AuthenticationFlowContext

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

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

@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服用UserSessionModel方法,那也是解决方案。

解决方法

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

在身份验证器中获取当前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();
    }