2013-07-17 33 views
0

我正在開發使用WorkLight 5.0.6的移動應用程序,並且我想將安全Cookie附加到由適配器返回的響應。將Cookie附加到WorkLight適配器響應標頭

我們不使用WorkLight身份驗證域,因爲我們不希望將會話「綁定」到羣集生產環境中的特定WL服務器。我們通過調用登錄適配器對會話進行身份驗證,該適配器通過後端系統驗證用戶詳細信息。作爲登錄適配器調用響應的一部分,我希望創建一個包含經過身份驗證的信息的安全cookie(僅限http),並將其附加到從登錄適配器返回的響應中。該cookie還應包含在從應用程序調用到服務器的後續適配器的標頭中。

問候,

Tom. 

回答

5

我建議想創建一個自定義的工作燈認證與後端通信。自定義認證文檔可以在這裏找到:

http://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v600/08_04_Custom_Authenticator_and_Login_Module.pdf

要回答你的問題,這裏是我會怎麼處理它無需使用自定義認證:

  • 使適配器的呼叫從認證客戶端

功能的authenticate(用戶名,密碼){

var invocationData = { 
      adapter : 'authenticationAdapter', 
      procedure : 'authenticate', 
      parameters : [username, password] 
    }; 

    WL.Client.invokeProcedure(invocationData, { 
     onSuccess : authSuccess, 
     onFailure : authFailure 
    });  

}

  • 獲取從客戶端的響應中的Cookie和保存它(我建議使用JSONStore也可以加密存儲的cookie保存)
function authSuccess(response){ 
    console.log("Auth Success"); 
    var myCookie = response.invocationResult.responseHeaders.CookieName 

    // Save cookie somehow 
} 
  • 開隨後的適配器的呼叫,從客戶機發送該cookie與每個請求一起

功能adapterRequestForProtectedResource(){

變種mySecureCookie = getMyCookieFromLocalStorage();

var invocationData = { 
      adapter : 'protectedResourceAdapter', 
      procedure : 'getResource', 
      parameters : [mySecureCookie] 
    }; 

    WL.Client.invokeProcedure(invocationData, { 
     onSuccess : success, 
     onFailure : failure 
    });  

}

  • 在適配器,設置cookie在頭

    功能的getResource(secureCookie){

    // Secure cookie must be of the form: "CookieName=cookievalue" 
    
    var input = { 
        method : 'get', 
        returnedContentType : 'json', 
        path : "/resource", 
        headers: {"Cookie": secureCookie} 
    }; 
    
    return WL.Server.invokeHttp(input); 
    

    }

+0

很好的回答! – Jxadro

+0

非常有用的答案....謝謝 – rags

相關問題