ChallengeHandler.isCustomResponse 和 handleChallenge 函数在 Worklight 中不起作用

ChallengeHandler.isCustomResponse and handleChallenge functions are not working in worklight

这是我的js代码。在这两个函数中,一个是 ChallengeHandler.isCustomResponse 另一个是 ChallengeHandler.handleChallenge

出于安全原因,我在这里使用 FormBasedAuthenticator

为 "sampleAppRealm" 创建 challengeHandler

这里我使用了名为"getValidate()"的HTTP适配器程序。此过程在没有 securityTest 的情况下工作,但在有 securityTest 的情况下,它不起作用...

我不知道哪里出了问题..我很困惑

我正在使用 Worklight 7.0 版 和 jquery 版本 1.11 和 jquery 移动版 1.4.5

我的js代码:

<script>
       $(document).ready(function(){
        alert("Hi");
       });
       function getValidate(){
        alert();
        var invocationData = {
      adapter : 'Go2needsHTTP',
      procedure : 'getValidate',
      parameters : []
     };
     var options = {
      onSuccess : getValidateSuccess,
      onFailure : getValidateFailure,
      invocationContext: {}
     };
     WL.Client.invokeProcedure(invocationData, options);
       }
       function getValidateSuccess(){
        alert("Success");
       }
       function getValidateFailure(){
        alert("Failure");
       }
       var SampleAppRealmChallengeHandler = WL.Client.createChallengeHandler("SampleAppRealm");  
       sampleAppRealmChallengeHandler.isCustomResponse = function(response) {
        alert(response);
        if (!response || response.responseText === null) {
            return false;
        }
        var indicatorIdx = response.responseText.search('j_security_check');
        
        if (indicatorIdx >= 0){
      return true;
     }  
     return false;
    }; 
    sampleAppRealmChallengeHandler.handleChallenge = function(response) {
     alert("handleChallenge");
        $('#AppDiv').hide();
        $('#AuthDiv').show();
        $('#AuthPassword').val('');
    };
    $(function(){
     $('#AuthSubmitButton').bind('click', function () {
      alert("AuthSubmitButton");
         var reqURL = '/j_security_check';
         var options = {};
         options.parameters = {
             j_username : $('#AuthUsername').val(),
             j_password : $('#AuthPassword').val()
         };
         options.headers = {};        sampleAppRealmChallengeHandler.submitLoginForm(reqURL, options, sampleAppRealmChallengeHandler.submitLoginFormCallback);
     });
    });
    $(function(){
     $('#AuthCancelButton').bind('click', function () {
      alert("AuthCancelButton");
         sampleAppRealmChallengeHandler.submitFailure();
         $('#AppDiv').show();
         $('#AuthDiv').hide();
     });
    });
      </script>

这是我的 html 代码:

我的html代码:

<div data-role="page" id="page">
       <div data-role="content" style="padding: 15px">
        <div id="AppDiv">
      <input type="button" id="getSecretDataButton" value="Call protected adapter proc" onclick="getValidate()" />
      <input type="button" class="appButton" value="Logout" onclick="WL.Client.logout('SampleAppRealm',{onSuccess: WL.Client.reloadApp})" />
      <div id="ResponseDiv"></div>
     </div>
        <div id="AuthDiv" style="display: block;">
      <p id="AuthInfo"></p>
      <div id="loginForm">
       <input type="text"`enter code here` id="AuthUsername" placeholder="Enter username" />
       <br/>
       <br/>
       <input type="password" id="AuthPassword" placeholder="Enter password" />
       <br/>
       <input type="button" id="AuthSubmitButton" onclick="WL.Client.Login('SampleAppRealm');" class="formButton" value="Login" />
       <input type="button" id="AuthCancelButton" class="formButton" value="Cancel" />
      </div>
     </div>
       </div>
      </div>

这是我的authenticationConfig.xml文件:

<customSecurityTest name="SampleAppRealm-securityTest"> <test realm="SampleAppRealm" isInternalUserID="true"/></customSecurityTest> <loginModule name="StrongDummy" expirationInSeconds="-1"> <className>com.worklight.core.auth.ext.NonValidatingLoginModule</className> </loginModule> <realm name="SampleAppRealm" loginModule="StrongDummy"> <className>com.worklight.core.auth.ext.FormBasedAuthenticator</className> </realm> 

这是我的 HTTPadapter 程序:

<procedure name="getValidate" securityTest="SampleAppRealm-securityTest"></procedure>

只是测试并告诉我哪里做错了无法找出错误。 请帮助我....

所以你是说 isCustomResponse 根本不会被触发?你的alert没有出现?

在这种情况下,我认为错误很简单:

您声明:var SampleAppRealmChallengeHandler = ...

然后你设置sampleAppRealmChallengeHandler.isCustomResponse = ...

注意第一个是大写 S 而第二个是小写 s。 Javascript 区分大小写。如果您以一种方式声明变量,则需要在所有地方都使用完全相同的外壳。