其他分享
首页 > 其他分享> > springMVC解决跨域

springMVC解决跨域

作者:互联网

一般的解决跨域都是用ajax,

1、代理
2、JSONP(响应头,只支持GET)
3、XHR2(XMLHttpRequest Level 2,在远程服务器设置response的Header)

这里直接用filter解决跨域。

@Component

public class ReWriteFilter implements Filter {

    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, HEAD");

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

        response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");

        chain.doFilter(req, res);

    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

 

标签:Control,跨域,springMVC,response,Access,Allow,解决,public,setHeader
来源: https://www.cnblogs.com/zsyIndex/p/15745175.html