前端修改
// 允许浏览器携带cookies访问
axios.defaults.withCredentials = true
后端创建一个过滤器
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CORSFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
response.setHeader("Access-Control-Max-Age", "3600");
//设置跨域
String headers = "Access-Control-Allow-Origin,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers";
response.setHeader("Access-Control-Allow-Headers", headers);
response.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(request, response);
}
public void destroy() {}
}
在main函数添加以下代码使过滤器生效
@Bean
public FilterRegistrationBean corsFilterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean(new CORSFilter());
return registrationBean;
}