【发布时间】:2014-07-06 17:07:19
【问题描述】:
我在 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 user 为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":[]}
【问题讨论】:
-
您能否提供一个演示此无限循环的示例项目,并提及您在哪里测试应用程序(设备?模拟器?预览?)
-
IDan:我刚刚开始工作。其实,我在这里等你回复。 :P。我很快就会发布答案。请提供您的评论,因为我的回答与 Worklight 无关。刚才一位 IBM 技术人员给我发邮件说“只需要在主 index.html 文件中进行 Javascript 引用”
-
Idan Adar:无限循环即将出现在模拟器上,因为我对我的代码有疑问,所以我没有继续使用实际设备。但是非法状态的问题:无法更改已登录用户的身份正在真实设备上。我试过 Moto-E 和客户端提供的 Android 硬件(不能透露客户端项目名称):D。但现在它的解决可能是包含该 handler.js 文件正在向服务器随机发送请求,而服务器并不期待它并返回非法状态:无法更改已登录用户的身份。
-
谢谢。请提供此作为问题的答案。
标签: ibm-mobilefirst worklight-adapters worklight-server