Ibm mobilefirst 如何在使用基于Worklight适配器的身份验证时获取注销用户的用户标识

Ibm mobilefirst 如何在使用基于Worklight适配器的身份验证时获取注销用户的用户标识,ibm-mobilefirst,worklight-adapters,worklight-security,Ibm Mobilefirst,Worklight Adapters,Worklight Security,我目前正在为我的Worklight应用程序实施基于适配器的身份验证。 作为记录,我使用的是Worklight 5.0.6.1版 正如文档中建议的那样,我想做的是在身份验证适配器的“注销”功能中执行一些清理 因此,在Worklight框架自动调用的注销函数中,我想检索包含注销用户信息的userIdentity对象。我试图通过调用“WL.Server.getActiveUser()”来实现这一点,但在注销函数中似乎不可能做到这一点 我可以在日志中看到以下异常(WebSphere App Server

我目前正在为我的Worklight应用程序实施基于适配器的身份验证。 作为记录,我使用的是Worklight 5.0.6.1版

正如文档中建议的那样,我想做的是在身份验证适配器的“注销”功能中执行一些清理

因此,在Worklight框架自动调用的注销函数中,我想检索包含注销用户信息的userIdentity对象。我试图通过调用“WL.Server.getActiveUser()”来实现这一点,但在注销函数中似乎不可能做到这一点

我可以在日志中看到以下异常(WebSphere App Server 7):

这背后的想法是,我想调用一个外部REST服务,该服务将在DB中执行一些清理,我需要将移动应用程序userId作为该服务的参数传递

是否有人可以提供一些最佳做法,以便从身份验证适配器注销功能中检索正在注销的用户的身份


谢谢。

在调用Adapter.onLogout()之前,底层身份验证框架会破坏用户身份。因此,在调用Adapter.onLogout()时,用户标识不再存在。因此,WL.Server.getActiveUser()将返回null或抛出异常(在您的情况下,因为它没有定义任何用户域,这很好)

若您仍然需要来自userIdentity的数据,即使底层auth框架放弃了它(这是您的情况),您可以将userIdentity保存在会话状态。但是,您需要记住,因为您是手动将其存储在那里的——一旦不再需要,您也有责任擦拭它

因此,适配器代码类似于:

/* global var, not inside of any function*/
var userIdentity = null;

function submitCredentials(user, pass){
    if (/*validate credentials*/){

        /* using previously created global var, not declaring it again */
        userIdentity = {
             userId:user,
             displayName:user
        };
        WL.Server.setActiveUser("realm", userIdentity);
    }
}

function onLogout(){
    /* do your stuff with userIdentity object and then wipe it*/
    userIdentity = null;
}

与常规适配器流的主要区别在于,userIdentity对象不是在submitCredentials()函数的作用域中创建的,而是作为全局变量创建的,因此它是会话作用域变量。

谢谢Anton!在您发表评论之前,我不知道每个经过身份验证的用户都有一个唯一的适配器实例。每个会话(无论其身份验证状态如何)都有自己的适配器实例。
/* global var, not inside of any function*/
var userIdentity = null;

function submitCredentials(user, pass){
    if (/*validate credentials*/){

        /* using previously created global var, not declaring it again */
        userIdentity = {
             userId:user,
             displayName:user
        };
        WL.Server.setActiveUser("realm", userIdentity);
    }
}

function onLogout(){
    /* do your stuff with userIdentity object and then wipe it*/
    userIdentity = null;
}