使用Vue-cli 和 Tomcat 7 编写的前后端分离项目 由于同源策略 axios 默认跨域请求不携带 cookie 并且 无法将响应头cookie写入浏览器 这导致每次请求都是一次新的会话 下面是解决办法
在 Vue 中 main.js 设置
axios.defaults.withCredentials=true
下面是设置tomcat 拦截器,
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) servletResponse;
HttpServletRequest req = (HttpServletRequest) servletRequest;
resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
resp.setHeader("Access-Control-Allow-Headers","Accept,Authorization,DNT,Content-Type,Referer,User-Agent");
resp.setHeader("Access-Control-Allow-Credentials", "true");
filterChain.doFilter(servletRequest, servletResponse);
}
设置以上两项就可以解决跨域cookie写入和携带问题
但 Access-Control-Allow-Headers 设置是什么意思,有没有朋友知道的?
|