解决方案
1、前端(Vue)配置
axios.defaults.withCredentials = true
2、后端(Java)配置
public class AllowOriginFilter implements Filter { @SuppressWarnings("unused") public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; HttpServletRequest request = (HttpServletRequest)req; // response.setHeader("Access-Control-Allow-Origin", "*"); // 设置允许所有跨域访问(不支持带Cookie) response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); // 设置允许所有跨域访问(支持带Cookie) response.setHeader("Access-Control-Allow-Credentials", "true"); // 配合前端 response.setHeader("Access-Control-Allow-Methods", "POST,GET,PUT,OPTIONS,DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept,Authorization,token"); chain.doFilter(req,res); } public void init(FilterConfig filterConfig) {} public void destroy() {} }
附:导致跨域问题的情况