解决 前后端分离 跨域 sessionid每次都变化

前端开发使用的VUE,后端使用的java,前后端分离,因为跨域问题JSESSIONID每次请求都会变化,解决方法如下:

前端要将withCredentials设为true

以ajax请求为例:

$.ajax({
   url: a_cross_domain_url,
   // 将XHR对象的withCredentials设为true
   xhrFields: {
      withCredentials: true
   }
});


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

        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.setHeader("Access-Control-Allow-Origin", "http://192.168.199.240:8081");
        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就一致了。

你可能感兴趣的:(js,java,ajax,java,javascript,跨域)