前段配置
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);
}
}