spring boot +vue + axios session不一致问题的解决方案

前段配置

main.js 的配置

import axios from 'axios'
axios.defaults.withCredentials = true

后端的配置

添加过滤器

- response.setHeader("Access-Control-Allow-Credentials","true"); 必须设置为true

@Component
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-type");
        response.setHeader("Access-Control-Allow-Credentials","true");
        System.out.println("*********************************过滤器被使用**************************");
        chain.doFilter(req, res);
    }
}
上一篇:mysql数据库的语法


下一篇:spring boot 使用 web Socket 的两种方式