0

我使用了基于表单的身份验证模块附带的示例质询处理程序。我根据我的要求修改了它。在我的应用程序中,我有一个登陆(主页)页面,其中有一个登录页面的链接。现在我希望它在用户单击登录按钮时起作用。我在这里面临各种问题:

  1. 在第一次单击登录按钮时,它会通过 WL 服务器进行身份验证,但随后无法继续执行登录功能逻辑。
  2. 第一次点击后,我必须第二次点击登录按钮。在第二次登录点击它完美地执行登录功能。
  3. 当我注销时,它不是从服务器中删除会话还是什么?它只执行注销功能代码(如下所示)。但是在指定的会话超时后再次提示第二/第三次并显示“超时消息”。虽然用户没有再次登录。他已经注销了。

目的:我希望应用程序仅在用户单击登录按钮时登录,但只需单击一下。当我注销或超时时,它不应保持会话活动或在指定时间后再次显示“会话超时”消息。

我的挑战处理程序:

var aahadAppRealmChallengeHandler = WL.Client.createChallengeHandler("myAppRealm");
var isLandingPage=false , islogout=false;
aahadAppRealmChallengeHandler.isCustomResponse = function(response) {
    WL.Logger.debug("I am here >> 1");
    if (!response || response.responseText === null) {  return false;    }
    var indicatorIdx = response.responseText.search('j_security_check');
    if (indicatorIdx >= 0){ WL.Logger.debug("return true "); return true; }  
    else { 
        if(isLandingPage && $.trim($('#fldloginUserID').val()) !="" && $.trim($('#fldloginUserPassword').val()) !="" ) {
             WL.Logger.debug("WL.Client.isUserAuthenticated()=" + WL.Client.isUserAuthenticated("myAppRealm"));
             if(WL.Client.isUserAuthenticated("myAppRealm")) {       WL.Logger.debug("return false ");  return false;           }
             else {  WL.Logger.debug("return true "); return true;          }
         } 
        WL.Logger.debug("return false ");  return false;
    }
};
aahadAppRealmChallengeHandler.handleChallenge = function(response) {
     WL.Logger.debug("I am here >> 2");
     var indicatorIdx = response.responseText.search('j_security_check');
     var suc = response.responseText.search('success');
     WL.Logger.debug("I am here >> 3 - indicatorIdx =" + indicatorIdx + " Success =" + suc + "  - isLandingPage=" + isLandingPage +" islogout=" +islogout);
    if (isLandingPage){
            if (suc >= 0 ){
                WL.Logger.debug("I am here >> 4 - isLandingPage=" + isLandingPage +" suc="+suc);
                var reqURL = '/j_security_check';           var options = {};
                options.parameters = {
                    j_username : $.trim($('#fldloginUserID').val().toLowerCase()),
                    j_password : $.trim($('#fldloginUserPassword').val())
                };
                options.headers = {};
                aahadAppRealmChallengeHandler.submitLoginForm(reqURL, options, aahadAppRealmChallengeHandler.submitLoginFormCallback);
            }else {
                WL.Logger.debug("I am here >> 5");
                WL.SimpleDialog.show(DialogMessages_en.SessionExpired_Tile, DialogMessages_en.SessionExpired_Description ,
                    [ { text : 'Close', handler : function () {                 
                        if(busyIndicator.isVisible())   
                            busyIndicator.hide();
                         isLandingPage = false; userLogout();islogout=true;
                        $.mobile.changePage("#landingPage" , { transition: "slide"});   
                    } } ]);
            }
    }
     else {
         WL.Logger.debug("I am here >> 6 - isLandingPage=" + isLandingPage +" re-Login Again");
         if(indicatorIdx < 1) {
                var reqURL = '/j_security_check';       var options = {};
                options.parameters = {
                    j_username : $.trim($('#fldloginUserID').val().toLowerCase()),
                    j_password : $.trim($('#fldloginUserPassword').val())
                };
                options.headers = {};
                aahadAppRealmChallengeHandler.submitLoginForm(reqURL, options, aahadAppRealmChallengeHandler.submitLoginFormCallback); 
         }
     }
};
aahadAppRealmChallengeHandler.submitLoginFormCallback = function(response) {
    var isLoginFormResponse = aahadAppRealmChallengeHandler.isCustomResponse(response);
    if (isLoginFormResponse){  isLandingPage=false; aahadAppRealmChallengeHandler.handleChallenge(response);  } 
    else {isLandingPage=true;  aahadAppRealmChallengeHandler.submitSuccess(); WL.Logger.debug("aahadAppRealmChallengeHandler.submitSuccess()");       }
};
$('#logindone').bind('click', function () {
    WL.Logger.debug(" Button Clicked -Before isLandingPage=" +isLandingPage);  
    isLandingPage=true;  
            var reqURL = '/j_security_check';       var options = {};
            options.parameters = {
                j_username : $.trim($('#fldloginUserID').val().toLowerCase()),
                j_password : $.trim($('#fldloginUserPassword').val())
            };
            options.headers = {};
            aahadAppRealmChallengeHandler.submitLoginForm(reqURL, options, aahadAppRealmChallengeHandler.submitLoginFormCallback);
            loginAuthenticateUser();        
});

我的登录功能

function loginAuthenticateUser() {
     WL.Logger.debug("Calling loginAuthenticateUser()....");
     busyIndicator.show();
     if ($.trim( $("#fldloginUserID").val()) !="" && $.trim( $("#fldloginUserPassword").val()) !="") {
            authenticateLDAPUsers( $.trim( $("#fldloginUserID").val().toLowerCase() ) , $.trim( $("#fldloginUserPassword").val() ));
     }else {
            if(busyIndicator.isVisible())   
                busyIndicator.hide();
       simpleDialogDemo(DialogMessages_en.LoginFailed_MsgTitle , DialogMessages_en.LoginFailed_MsgDescription);
     }
}

登出功能

function userLogout() {
      WL.Logger.debug("Logout....");
      WL.TabBar.setVisible(false);
      WL.Client.logout('myAppRealm', {onSuccess: function(){}  });  
      $.mobile.changePage("#landingPage" , { transition: "slide"});
      var options = {onSuccess: function() {WL.Logger.debug("collection closed");}, onFailure: function() { WL.Logger.debug("collection closing failed"); } };
      WL.JSONStore.closeAll(options);
}

authenticationConfig.xml(领域)

<realm loginModule="Strongme" name="myAppRealm">
            <className>com.worklight.core.auth.ext.FormBasedAuthenticator</className>
            <parameter name="login-page" value="login.html" />
        </realm>

Worklight.properties

serverSessionTimeout=5

请有任何建议。

谢谢

4

1 回答 1

2

In case of FormBasedAuthentication you need to trigger authentication before actually submitting credentials. Therefore you need to call WL.Client.login("realm-name") in your app.

In case authentication requires immediately on app startup - call WL.Client.login(..) in your wlEnvInit or wlCommonInit function. In case it is requires on a later stage - call it once you need it.

于 2013-05-28T09:42:50.863 回答