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

Ibm mobilefirst IBM worklight:调用适配器时setActiveUser()出错,ibm-mobilefirst,worklight-adapters,worklight-server,Ibm Mobilefirst,Worklight Adapters,Worklight Server,我在worklight server上遇到一个奇怪的异常,说明过程调用错误。非法状态:无法更改域“SingleStepAuthRealm”中已登录用户的身份。应用程序必须先注销。我正在使用单步执行适配器身份验证。张贴所有代码,请帮助我了解我在哪里搞砸了。 ************************************SingleStepAuthAdapter-impl.js*************************** function onAuthRequired(heade

我在worklight server上遇到一个奇怪的异常,说明过程调用错误。非法状态:无法更改域“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");
}
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));
}
<?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>
*************************************SingleStepAuthRealmChallengeProcessor.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");
}
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));
}
<?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>
*****************************SingleStepAuthAdapter.xml********************************

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");
}
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));
}
<?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>

我有文件,即Registration.html、Login.html和Index.html。在这里,我将“SingleStepAuthRealmChallengeProcessor.js”包含在所有文件中,如

所以,为了尝试不同的方法,我只从Registration.html和Login.html文件中删除了它,然后重新运行了它。而且它很有魅力

这可能是由于SingleStepAuthRealmChallengeProcessor.js文件随机向服务器发送请求,而服务器不希望收到该请求,并返回非法状态:无法更改已登录用户的身份


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

你能提供一个示例项目来演示这个无限循环,并提到你在哪里测试这个应用程序(设备?模拟器?预览?)。实际上,我在这里等你的答复我很快就会回复。请提供您的意见,因为我的回答与Worklight无关。刚才一位IBM技术人员给我发邮件说“Javascript引用只需要在主index.html文件中进行”Idan Adar:无限循环是在模拟器上进行的,因为我对我的代码有疑问,所以我没有在实际设备上进行。但非法状态的问题是:无法更改实际设备上已登录用户的身份。我在Moto-E和客户端提供的Android硬件上进行了尝试(不能透露客户端项目名称):D。但现在解决的问题可能是,handler.js文件中包含的内容随机向服务器发送请求,服务器不希望收到该请求,并返回非法状态:无法更改已登录用户的身份。谢谢。请提供此作为问题的答案。