spring mvc cors跨域全局过滤器

1.写filter过滤器

public class CorsFilter implements Filter {

@Override

public void init(FilterConfig filterConfig) throws ServletException {

}

@Override

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

HttpServletResponse res = (HttpServletResponse) response;

res.setHeader("Access-Control-Allow-Origin", "*");

res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");

res.setHeader("Access-Control-Max-Age", "3600");

res.setHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, token");

res.setHeader("Access-Control-Allow-Credentials", "true");

chain.doFilter(request, response);

}

@Override

public void destroy() {

}

}

2.把filter注册到web.xml中 即可

你可能感兴趣的:(spring mvc cors跨域全局过滤器)