解决跨域问题

时间:2024-03-29 20:17:39
@Component public class CorsFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 将 ServletResponse 转换为 HttpServletResponse HttpServletResponse httpResponse = (HttpServletResponse) response; // 设置允许跨域请求的源地址 httpResponse.setHeader("Access-Control-Allow-Origin", "*"); // 设置允许的请求方法 httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); // 设置允许的请求头 httpResponse.setHeader("Access-Control-Allow-Headers", "*"); // 继续执行 Filter 链 chain.doFilter(request, response); } }