@Component
public class OriginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { }
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
// 响应类型
httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
// 预检请求的结果缓存60分钟
httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
//httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
// 允许所有
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
// 指定允许其他域名访问
// response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1,http://locahost"); // 允许白名单IP
// 响应头设置
httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
filterChain.doFilter(request, response);
}
@Override
public void destroy() { }
}
允许跨域配置
最新推荐文章于 2024-07-06 09:00:00 发布