IBM worklight:调用适配器时setActiveUser()出错

时间:2014-07-06 17:07:19

标签: ibm-mobilefirst worklight-adapters worklight-server

我在worklight服务器上收到一个奇怪的异常,指出过程调用错误。非法国家:无法在领域中更改已登录用户的身份' SingleStepAuthRealm'。应用程序必须首先注销。我正在使用单步执行适配器身份验证。发布所有代码请帮助我了解我搞砸的地方。 ************************************ SingleStepAuthAdapter-impl.js ********* ******************

function onAuthRequired(headers, errorMessage){
    WL.Logger.debug("Inside adapter.js onAuthRequired");
    errorMessage = errorMessage ? errorMessage : null;

    return {
        authRequired: true,
        errorMessage: errorMessage
    };  
}

function loginAuthentication(username, password,returnvalue){

    WL.Logger.debug("Inside loginAuthentication");

    var returned = WL.Server.invokeSQLStoredProcedure({
        procedure : "loginAuthentication",
        parameters : [username,password,returnvalue]
    });

    var isAuth = (returned.resultSet[0].returnvalue == 1);

    if (isAuth){
        //WL.Logger.debug("Inside loginAuthentication Authentication Successful "+JSON.stringify(WL.Server.getActiveUser("SingleStepAuthRealm")));

        var userIdentity = {
                userId: username,
                displayName: username
        };

        WL.Server.setActiveUser("SingleStepAuthRealm", userIdentity);   

        return {            
            authRequired: false 
        };
        WL.Logger.debug("Inside loginAuthentication Authentication Successful returned authRequired false");
}

    return onAuthRequired(null, "Invalid Login Credentials");
}

function getSecretData(){
    WL.Logger.debug("Inside adapter.js getSecretData");
    return {
        secretData: "Authentication Done and its a secret data"
    };
}

function onLogout(){
    WL.Logger.debug("Inside adapter.js onLogout");
    WL.Server.setActiveUser("SingleStepAuthRealm", null);
    WL.Logger.debug("Logged out");
}

*************** SingleStepAuthRealmChallengeProcessor.js ******* ****

var singleStepAuthRealmChallengeHandler = WL.Client.createChallengeHandler("SingleStepAuthRealm");

singleStepAuthRealmChallengeHandler.isCustomResponse = function(response) {
    console.log("Inside singleStepAuthRealmChallengeHandler.isCustomResponse "+response +" :: "+ !response.responseJSON +" :: "+ response.responseText);
    if (!response || !response.responseJSON || 
            response.responseText === null) {
        return false;
    }
    console.log("Inside response.responseJSON.authRequired "+response.responseJSON.authRequired);
    if (typeof(response.responseJSON.authRequired) !== 'undefined'){
        return true;
    } else {
        return false;
    }
};

singleStepAuthRealmChallengeHandler.handleChallenge = function(response){

    var authRequired = response.responseJSON.authRequired;
    WL.Logger.debug("Inside singleStepAuthRealmChallengeHandler.handleChallenge :: response.responseJSON.authRequired ");

    /*if(WL.Client.isUserAuthenticated("SingleStepAuthRealm") == false)
    {
        WL.Client.logout("SingleStepAuthRealm");
    }*/

    if (authRequired == true){
        WL.Logger.debug(" Inside authRequired == true");

        // 1.b else display up login screen
        console.log("Login Returned false");
        alert("Already Registered, Please login to continue");
        $("#pagePort").load(path + "pages/Login.html", function() 
                {
                    $.getScript(path+ "js/Login.js",function() {
                        if (currentPage.init) 
                        {
                            currentPage.init();
                        }
                        });
                        //$.getScript(path+ "js/SingleStepAuthRealmChallengeProcessor.js",function() {});
                                });

        if (response.responseJSON.errorMessage)
            {
            alert("Problem "+response.responseJSON.errorMessage);
            }

    } 
    else if (authRequired == false)
    {   


        WL.Logger.debug(" Inside authRequired == false "+WL.Client.isUserAuthenticated("SingleStepAuthRealm"));


        var userName = "Random";//loginResultArr[0].json.uName;
        console.log("Username "+ userName);
            // 1.a if login data exists directly go to home page see
            console.log("Login Returned true");
            appUsernameGlobal = userName;
            $("#pagePort").load(path+ "pages/MainMenu.html",function() {
                $.getScript(path+ "js/MainMenu.js", function() {
                    if (currentPage.init) {
                        currentPage.init();
                        }
                        });
                });

        singleStepAuthRealmChallengeHandler.submitSuccess();
    }
};


function loginClick() {
    WL.Logger.debug(" Inside AuthSubmitButton");
    var username = $("#init-username").val();
    var password = $("#init-password").val();
    var returnvalue = 0;

    var invocationData = {
        adapter : "SingleStepAuthAdapter",
        procedure : "loginAuthentication",
        parameters : [username, password,returnvalue]
    };

    WL.Logger.debug(" before submitAdapterAuthentication");
    singleStepAuthRealmChallengeHandler.submitAdapterAuthentication(invocationData, {onSuccess: getLoginAuthenticationOK, onFailure: getLoginAuthenticationFAIL});
    WL.Logger.debug(" after submitAdapterAuthentication");

}


function getLoginAuthenticationOK(response){
    WL.Logger.debug("Inside SingleStepAuthenticationRealmChallenge.js getLoginAuthenticationOK :: secret data is :: " + JSON.stringify(response.invocationResult));
}  

function getLoginAuthenticationFAIL(response){
    WL.Logger.debug("Inside SingleStepAuthenticationRealmChallenge.js getLoginAuthenticationFAIL "+JSON.stringify(response.invocationResult));
}

******** SingleStepAuthAdapter.xml *************** *****************

<?xml version="1.0" encoding="UTF-8"?>

<wl:adapter name="SingleStepAuthAdapter"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:wl="http://www.worklight.com/integration"
    xmlns:sql="http://www.worklight.com/integration/sql">

    <displayName>SingleStepAuthAdapter</displayName>
    <description>SingleStepAuthAdapter</description>
    <connectivity>
        <connectionPolicy xsi:type="sql:SQLConnectionPolicy">
            <dataSourceDefinition>
                <driverClass>com.mysql.jdbc.Driver</driverClass>
                <url>jdbc:mysql://192.168.xx.xx:3306/project</url>
                <user>root</user>
                <password>root</password>
            </dataSourceDefinition>         
        </connectionPolicy>
        <loadConstraints maxConcurrentConnectionsPerNode="10" />
    </connectivity>

    <procedure name="loginAuthentication"/>
    <procedure name="getSecretData" securityTest="SingleStepAuthAdapter-securityTest"/>     

</wl:adapter>

Worklight - How to check if a client is already logged in, then pass the login screen中,它表示将setActive用户设置为null,但在激活Server进入无限循环之前设置为null。我想知道,如果我没有设置任何活动用户,而不是服务器说应用程序必须首先注销?我尝试了我的水平,但没有让我的问题解决。

{"errors":["Illegal State: Cannot change identity of an already logged in user in realm 'SingleStepAuthRealm'. The application must logout first."],"isSuccessful":false,"warnings":[],"info":[]} 

1 个答案:

答案 0 :(得分:1)

我有文件,即Registration.html,Login.html和Index.html。在这里,我收录了&#34; SingleStepAuthRealmChallengeProcessor.js&#34;在所有文件中,如<script src="js/ SingleStepAuthRealmChallengeProcessor.js"></script>

所以,只是尝试不同,我只从Registration.html和Login.html文件中删除它并重新运行它。它就像魅力一样。

可能包含此SingleStepAuthRealmChallengeProcessor.js文件随机向服务器发送请求,服务器没有期待它并返回非法状态:无法更改已登录用户的身份。

刚才我通过IBM技术人员了解到js引用只需要在@ Index.html上完成