前后端分离架构中如何解决 Session 无法跨域共享

前端 Ajax 中:
 $.ajax({ 
     url:url, //加上这句话 
	 xhrFields: {
	    withCredentials: true 
	  }, 
	 crossDomain: true, 
	 success:function(result){ 
		 alert("test"
		 );
	  },
	 error:function(){
	  }
   });
	服务端配置过滤器,在 doFilter 中进行处理: 
	HttpServletResponse res = (HttpServletResponse) servletResponse; 
	HttpServletRequest request=(HttpServletRequest)servletRequest; res.setContentType("textml;charset=UTF-8"); 
	res.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
	res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); 
	res.setHeader("Access-Control-Max-Age", "0"); 
	res.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With,
	If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");
	res.setHeader("Access-Control-Allow-Credentials", "true"); 
	res.setHeader("XDomainRequestAllowed","1"); filterChain.doFilter(servletRequest,servletResponse);

你可能感兴趣的:(web)