前后端分离 跨域 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设置:

前后端分离 跨域 sessionid保持一致

后端设置,以java为例,其他语言类似:

[java] view plain copy
  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,前后端分离,解决方法如下:

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8" />  
  5.         <title></title>  
  6.         <script type="text/javascript" src="js/jquery-1.9.0.js" ></script>  
  7.         <script type="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.               
  33.             function huoqu(){  
  34.                 alert("进入单击事件");      
  35.                 $.ajax({  
  36.                     xhrFields: {  
  37.                         withCredentials: true  
  38.                     },  
  39.                     type:"get",  
  40.                     date:{},  
  41.                     url:"http://localhost/api/admin/login/getSessionAdmin",  
  42.                     success: function(data){  
  43.                         alert(data);  
  44.                         alert("成功");  
  45.                     }  
  46.                 });  
  47.             }  
  48.         </script>  
  49.     </head>  
  50.     <body>  
  51.             用户名:<input type="text" id="adminUser" name="adminUser"/><br />  
  52.             密码:<input type="text" id="passWord" name="passWord"/><br />  
  53.             <input type="button" id="tj" value="登录" onclick="denglu();"/>  
  54.             <input type="button" onclick="huoqu();" value="查询当前seesion中的管理员"/>  
  55.     </body>  
  56. </html>  


主要在于get、post提交时参数的问题

get提交

前后端分离 跨域 sessionid保持一致


post提交

前后端分离 跨域 sessionid保持一致