2014-02-18 101 views
0

我正在呼叫创建来自SAPUI5的SAP Net weaver网关的服务。我已经写了下面的代码获取CSRF,但我无法得到它。它要求我输入图像中显示的用户名和密码。在SAPUI5中X-CSRF-TOKEN验证失败

代码:

function onSave() 
{ 
    useroDataModel = newsap.ui.model.odata.ODataModel("proxy/sap/opu/odata/sap/ZUI_GROUP1_CREATE"); 

    empid = oTF2.getValue(); 
    empname = oTF1.getValue(); 
    empaddr = oTF.getValue(); 
    empdoj = oDP.getValue(); 

    uilogon(); 

    useroDataModel.read("/zui_group1_createCollection(im_emp_id='"+empid+"')", null, null, true, fnSuccess, fnError); 

    useroDataModel.setHeaders({ 
     "X-CSRF-Token": "Fetch" // auth 
    }); 

    function fnSuccess(data,response) 
    { 
     //alert(response.headers['x-csrf-token']); 
     var header_xcsrf_token = response.headers['x-csrf-token']; 
     //alert(header_xcsrf_token); 
     var createrequestdata = { 
      im_emp_id : empid, 
      im_emp_name : empname(), 
      im_emp_addr : empaddr(), 
      im_emp_doj : empdoj() 
     }; 

     useroDataModel.setHeaders({ 
      "X-Requested-With": "XMLHttpRequest",       
      "Content-Type": "application/atom+xml", 
      "DataServiceVersion": "2.0", 
      "Accept": "application/atom+xml,application/atomsvc+xml,application/xml", 
      "X-CSRF-Token": header_xcsrf_token 
     }); 

     useroDataModel.create("/zui_g2_createCollection", createrequestdata, null, fnS, fnE); 

     function fnS(response){ 
      if(response.ex_status == "Entry Created.") 
       { 
       alert("Created."); 
       } 
      else 
       { 
       alert("Failed."); 
       } 
     } 
     function fnE(Error){ 
      alert("error in create"+Error.message); 
     } 

    } 

    function fnError(oError) 
    { 
     alert("Error in read"+oError.message); 
    } 
} 

function uilogon() 
{ 
    var tok = "username" + ':' + "password"; 
    var hash = btoa(tok); 
    auth = "Basic" + hash; 
// alert(auth); 
    // Save to Local Storage 
// $.sap.require("jquery.sap.storage"); 
// var UI5Storage = $.sap.storage(jQuery.sap.storage.Type.session); 
// UI5Storage.remove("Auth"); 
// UI5Storage.put("Auth",auth); 
    useroDataModel.setHeaders({ 
     //'Accept-Encoding': "gzip", 
     "Authorization" : auth}); 
// alert("dne"); 
} 

我能够在AUTH变量来获取CSRF-TOKEN。但它要求我进行以下身份验证,要求我输入SAP NET WEAVER GATEWAY的用户名和密码,但如果输入了我登录的用户名和密码,则不能接受。 authentication authentication

回答

3

如果你想用你的身份验证方法,你将需要之前ODataModel实例验证,你不能读取的元数据更不用说获取一个CSRF令牌,除非认证,还需要获取之前令牌做POST。

为什么不将用户名和密码进入的ODataModel

var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, bJSON, sUser, sPwd); 

如果您使用绑定功能CSRF安全令牌将被读为您否则,如果你想使用oModel.read一旦认证构造你可以手动做

oModel.refreshSecurityToken()