WL.Server.getActiveUser返回Null

时间:2016-02-17 21:39:35

标签: ibm-mobilefirst mobilefirst-adapters

我需要能够在java适配器中获取用户登录用户ID /密码。在阅读了大量文章之后,最好的方法似乎是打电话

来自java适配器调用的javascript函数的

WL.Server.getActiveUser。所以,我在http适配器上添加了一个 getIdentity 函数来验证我们的应用。我已经验证了getActiveUser在登录页面调用的身份验证功能中工作...

当我调用getIdentity函数时,getActiveUser使用相同的身份验证领域返回null。我在application_descriptor文件中设置了域。不知道我还有什么要做的。有什么想法吗?

function performAuthentication(username, password) {
       WL.Logger.info("In performAuthentication: username = " + username + " password = " + password + "Time = " + new Date(new Date().getTime()).toLocaleString());

        var invocationData = {
            adapter : 'BluePages',  
            procedure : 'authenticate',  
            parameters  : [username, password]
        };
        var invocationResult = WL.Server.invokeProcedure(invocationData);

        var fullName = invocationResult.result.fullName;
       if (invocationResult.result.success == false) {
            return {
                authRequired: true,
                loginPassed: false
            };
        } 
        else {
            userIdentity = {
                userId: username,
                credentials: password,
                displayName: username,
                attributes: {
                    foo: "bar"
                }
        };

        WL.Server.setActiveUser("AuthRealm", null);
        WL.Server.setActiveUser("AuthRealm", userIdentity);
        var activeUser = WL.Server.getActiveUser("AuthRealm");
        WL.Logger.info("activeUser = " + activeUser);

        if(activeUser && activeUser.userId == username){
            WL.Logger.info("Active userId = " + activeUser.userId + " password = "    + activeUser.credentials);
            WL.Logger.info("User has been logged in!");
            return {
                loginPassed: true,
                authRequired: false,
                fullName: fullName,
                result: invocationResult.result
            };
        }
        else {
            WL.Logger.info("Else Clause...");
            if(activeUser != null)
                WL.Server.setActiveUser("AuthRealm", null);
            WL.Server.setActiveUser("AuthRealm", userIdentity);
        }


         return {
            authRequired: false,
            loginPassed: true,
            fullName: fullName
        };
    }

 }


function getIdentity() {
        WL.Logger.info("AuthAdapter: In getIdentity: Time = " + new Date(new Date().getTime()).toLocaleString());

        WL.Logger.info("AuthAdapter: userIdentity = " + userIdentity);

        var activeUser = WL.Server.getActiveUser("AuthRealm");

        WL.Logger.info("AuthAdapter: getIdentity: getActiveUser returned = " + activeUser);

        if (activeUser) {

                WL.Logger.info("AuthAdapter: getIdentity userId = " + activeUser.userId);

                return {
                    userId: activeUser.userId,
                    credentials: activeUser.credentials,
        };
     }
 }

1 个答案:

答案 0 :(得分:5)

使用WL.Server.getActiveUser:

时,有两个理由可以获得null

1)如果在适配器上没有定义域,则该方法返回null(活动用户未知)

2)如果在适配器上定义了领域:

如果没有与用户关联的强身份(用户在此会话或之前的会话中进行了身份验证),则该方法返回null。

在你的情况下,你说领域是存在的所以我建议尝试#2

您可以在此处找到更多信息: https://www.ibm.com/support/knowledgecenter/SSZH4A_6.0.0/com.ibm.worklight.help.doc/apiref/r_method_wl_server_getactiveuser.html