亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決前后端分離 vue+springboot 跨域 session+cookie失效問題

 更新時(shí)間:2019年05月13日 10:50:22   作者:晨風(fēng)啊  
這篇文章主要介紹了前后端分離 vue+springboot 跨域 session+cookie失效問題的解決方法,解決過程也很簡單 ,需要的朋友可以參考下

環(huán)境:

前端 vue ip地址:192.168.1.205

后端 springboot2.0 ip地址:192.168.1.217

主要開發(fā)后端。

問題:

首先登陸成功時(shí)將用戶存在session中,后續(xù)請求在將用戶從session中取出檢查。后續(xù)請求取出的用戶都為null。

解決過程:

首先發(fā)現(xiàn)sessionID不一致,導(dǎo)致每一次都是新的會(huì)話,當(dāng)然不可能存在用戶了。然后發(fā)現(xiàn)cookie瀏覽器不能自動(dòng)保存,服務(wù)器響應(yīng)set-cookie了

搜索問題,發(fā)現(xiàn)跨域,服務(wù)器響應(yīng)的setCookie瀏覽器無法保存,而且就算保存了域名不同也不能攜帶。

第一步:

后臺(tái)添加過濾器,因?yàn)榍昂蠖朔蛛x,不可能每個(gè)方法都寫一遍,所以添加過濾器統(tǒng)一處理。

package com.test.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = "/*", filterName = "CORSFilter")
public class CORSFilter implements Filter {
 @Override
 public void destroy() {
 }
 /**
  * 此過濾器只是處理跨域問題
  * @param servletRequest
  * @param servletResponse
  * @param chain
  * @throws ServletException
  * @throws IOException
  */
 @Override
 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException {
  HttpServletRequest req = (HttpServletRequest) servletRequest;
  HttpServletResponse resp = (HttpServletResponse) servletResponse;
  String origin = req.getHeader("Origin");
  if(origin == null) {
   origin = req.getHeader("Referer");
  }
  resp.setHeader("Access-Control-Allow-Origin", origin);//這里不能寫*,*代表接受所有域名訪問,如寫*則下面一行代碼無效。謹(jǐn)記
  resp.setHeader("Access-Control-Allow-Credentials", "true");//true代表允許攜帶cookie
  chain.doFilter(servletRequest,servletResponse);
 }
 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
 }
}

springboot2.配置過濾器時(shí),啟動(dòng)類必須加上@ServletComponentScan才會(huì)加載過濾器

@SpringBootApplication
@EnableTransactionManagement(order = 10)
@ServletComponentScan
public class Application {
 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 }
}

然后前端配置

使用vue.resource發(fā)送請求時(shí)配置如下:
main.js中
Vue.http.options.xhr = { withCredentials: true }
使用vue.axios發(fā)送請求時(shí)配置如下:
axios.defaults.withCredentials = true;
jquery請求帶上 xhrFields: {withCredentials: true}, crossDomain: true;
$.ajax({
 type: "post",
 url: "",
 xhrFields: {withCredentials: true},
 crossDomain: true,
 data: {username:$("#username").val()},
 dataType: "json",
 success: function(data){ }
});

此時(shí)問題已解決。

但我查看請求時(shí),還是沒有帶cookie,太糾結(jié)于這一點(diǎn)了。以至于查看全部cookie時(shí)突然明白了。

沒有帶cookie。

瀏覽器全部cookie

已經(jīng)有服務(wù)器的cookie了。當(dāng)向服務(wù)器發(fā)送請求時(shí),會(huì)攜帶cookie,證明是同一會(huì)話。

發(fā)現(xiàn)火狐的請求頭中存在cookie,不知道為什么谷歌的請求頭不顯示,不明白。望解答。

總結(jié)

以上所述是小編給大家介紹的解決前后端分離 vue+springboot 跨域 session+cookie失效問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

最新評論