如何在使用基于Worklight适配器的身份验证时获取要注销的用户的userIdentity



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

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

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

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

[9/3/13 17:13:11:683 IST] 00000039 DataAccessSer 1        com.worklight.integration.services.impl.DataAccessServiceImpl invokeProcedureInternal Procedure 'onLogout' invocation failed. Runtime: Adapter 'onLogout' security test has no user realm.java.lang.RuntimeException: Adapter 'onLogout' security test has no user realm.

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

请有人提供一些最佳实践,以便从身份验证适配器注销功能中检索被注销用户的身份?

谢谢。

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

如果您仍然需要来自userIdentity的数据,即使底层身份验证框架丢弃了它(这就是您的情况),您也可以将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()函数的范围内创建的,而是作为一个全局变量创建的,因此它是一个会话范围的变量。

最新更新