前後端分離專案,支援跨域,session不丟失

2021-08-15 03:48:41 字數 1062 閱讀 3425

在除錯乙個前後端分離的專案,發現登入成功後已經把資訊儲存到session中了,再從session中取得當前使用者資訊的時候發現取不到,debug發現sessionid已經變了,自己網上查了很多資料,並不準確,

把自己的解決分享給大家:

後端***:

//***新增跨域支援(如果是web.xml配置***,請將@component刪除)

//@component

public class corsfilter implements filter

@override

public void

dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception

@override

public void

destroy()

}

關鍵點:

response.setheader("access-control-allow-origin", request.getheader("origin"));

response.setheader("access-control-allow-credentials","true"); //是否支援cookie跨域

前段**:

request:function(param) ,

type: param.method || 'get'

,url: param.url || ''

,datatype: param.type || 'json'

,data: param.data || ''

,success:function(res) ,

error:function(err)

});},

關鍵點:

在ajax請求中增加

xhrfields: ,

前後端專案分離 跨域問題

1.跨域 說明最少有兩個域,才會出現跨的這種情況 2.跨域問題出現的本質 a.網域名稱不同,b.埠號不同,c.協議 不同 3,瀏覽器的同源策略 非位址列請求 如a頁面在a服 務器,b頁面在b伺服器,在a頁面請求b頁面的資訊就會出現跨域問題,目的是為了保證使用者資訊保安 如果a頁面可以獲取b頁面資訊,...

前後端分離專案,請求跨域解決

前端傳送請求,產生跨域問題 created 控制台報錯 在後端設定允許跨域請求 package com.demo.demobackend.config import org.springframework.context.annotation.bean import org.springframew...

前後端分離專案涉及跨域問題

1.不使用jsonp,只修改服務端 response.setheader access control allow origin reqs.getheader origin response.setheader access control allow credentials true respon...