最近使用VUE开发前后端分离,登录后,后端存入用户信息到session,前端http axios异步请求获取不到sesion,查看http请求头发现http headers未携带cookie,
然后查资料后按照以下步骤解决,记录下,怕忘记...
1.后台servlet过滤器在原有允许跨域基础上增加
response.setHeader("Access-Control-Allow-Credentials", "true");//允许携带cookie
此时Access-Control-Allow-Origin不能设置*,否则前端访问后端接口会报错提示,
//当Access-Control-Allow-Credentials为true是 Access-Control-Allow-Origin不能为*response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8080"); //解决跨域访问报错response.setHeader("Access-Control-Allow-Methods", "POST,