跨域CORS支持

1、SpringMVC 4.2以上版本。

2、添加sping配置:

   
       
   

3、添加过滤器CORSFilter:

@Component

public class CORSFilter implements Filter{

     public void destroy() {}

     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)  

     throws IOException, ServletException {

         HttpServletResponse response = (HttpServletResponse) res; 

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

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

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

        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 

    chain.doFilter(req, res);

     }

     public void init(FilterConfig arg0) throws ServletException {}

}

4、web.xml中添加配置

    cors

    com.account.admin.filter.CORSFilter

    cors

    /*

你可能感兴趣的:(跨域CORS支持)