本文僅記錄如標題所述場景的測試所得,因爲場景有些特殊,且並不需兼容全部瀏覽器,因此本文的內容對讀者也許並沒有做用,僅爲記錄。ajax
基於歷史緣由:跨域
服務端響應:瀏覽器
httpResponse.setHeader("Access-Control-Allow-Origin", "http://example.com:8080"); httpResponse.addHeader("Access-Control-Allow-Credentials", "true"); httpResponse.addHeader("Access-Control-Allow-Methods", "HEAD,POST,GET,PUT,DELETE,OPTIONS");
客戶端Ajax請求時設置withCredentials
參數爲true
,記得返回Cookie的首次請求和其它請求都需設置:服務器
function login() { $.ajax({ url : urlPrefix + "/LoginServlet", type : "post", xhrFields: { withCredentials : true }, success : function(data, name) { alert(data) } }); } function test() { $.ajax({ url : urlPrefix + "/BusinessServlet", type : "post", xhrFields: { withCredentials : true }, success : function(data, name) { alert(data) } }); }
測試過程:
A項目部署在a機器,B項目部署在a機器,B項目的頁面跨域訪問A項目
測試結果:post
設置
-> Safari
-> 阻止Cookie
-> 始終禁止
纔不能經過Cookie傳遞SessionID測試過程:
A項目部署在a機器,B項目部署在b機器,B項目的頁面跨域訪問A項目。
若是a機器和b機器同屬同一局域網:測試
設置
-> Safari
-> 阻止Cookie
-> 始終容許
、容許訪問過的網站
、僅容許當前網站
能經過Cookie傳遞SessionID若是a機器和b機器其中之一屬於外網:網站
設置
-> Safari
-> 阻止Cookie
-> 始終容許
才能經過Cookie傳遞SessionID目前還沒探索出的緣由,根據測試結果,可能會改成其它實現方法,好比單點登陸後用JWT驗證會話,用JSONP跨域。url