1. 程式人生 > >前後端分離 跨域 sessionid保持一致

前後端分離 跨域 sessionid保持一致

1.前端開發使用的VUE,後端使用的java,前後端分離,解決方法如下:

前端要將withCredentials設為true

以ajax請求為例:

[javascript] view plain copy
  1. $.ajax({  
  2.    url: a_cross_domain_url,  
  3.    // 將XHR物件的withCredentials設為true
  4.    xhrFields: {  
  5.       withCredentials: true
  6.    }  
  7. });  

vue設定:


後端設定,以java為例,其他語言類似:

  1. httpResponse.setHeader("Access-Control-Allow-Credentials"
    "true");  
  2. httpResponse.setHeader("Access-Control-Allow-Origin""http://192.168.199.240:8081");  
  3. httpResponse.setHeader("Access-Control-Allow-Headers""Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");  

Access-Control-Allow-Credentials 設為true的話,Access-Control-Allow-Origin就不能設為*了,只好改成具體的域了,這樣就可以多次請求取到的sessionid就一致了。

前端開發使用的jquery,後端使用的java,前後端分離,解決方法如下:

  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <metacharset="utf-8"/>
  5.         <title></title>
  6.         <scripttype="text/javascript"src="js/jquery-1.9.0.js"></script>
  7.         <scripttype="text/JavaScript">
  8.             function denglu(){  
  9.                 alert("進入單擊事件");      
  10.                 var username=document.getElementById("adminUser").value;  
  11.                 alert("username:"+username);  
  12.                 var pass=document.getElementById("passWord").value;  
  13.                 alert("password:"+pass);  
  14.                 $.ajax({  
  15.                     contentType:'application/json',  
  16.                     xhrFields: {  
  17.                         withCredentials: true  
  18.                     },  
  19.                     type:"post",  
  20.                     data: JSON.stringify({  
  21.                         adminUser:username,  
  22.                         passWord:pass  
  23.                     }),  
  24.                     url:"http://localhost/api/admin/login/loginAdmin",  
  25.                     success: function(data){  
  26.                         alert(data);  
  27.                         alert("成功");  
  28.                         //window.location.href = 'main.html';  
  29.                     }  
  30.                 });  
  31.             }  
  32.             function huoqu(){  
  33.                 alert("進入單擊事件");      
  34.                 $.ajax({  
  35.                     xhrFields: {  
  36.                         withCredentials: true  
  37.                     },  
  38.                     type:"get",  
  39.                     date:{},  
  40.                     url:"http://localhost/api/admin/login/getSessionAdmin",  
  41.                     success: function(data){  
  42.                         alert(data);  
  43.                         alert("成功");  
  44.                     }  
  45.                 });  
  46.             }  
  47.         </script>
  48.     </head>
  49.     <body>
  50.             使用者名稱:<inputtype="text"id="adminUser"name="adminUser"/><br/>
  51.             密碼:<inputtype="text"id="passWord"name="passWord"/><br/>
  52.             <inputtype="button"id="tj"value="登入"onclick="denglu();"/>
  53.             <inputtype="button"onclick="huoqu();"value="查詢當前seesion中的管理員"/>
  54.     </body>
  55. </html>


主要在於get、post提交時引數的問題

get提交


post提交