Springboot接口开启跨域访问

作者: 老胡聊聊天 | 来源:发表于2017-10-15 16:57 被阅读9次

    后端使用Springboot提供rest接口,前端html和ios共用,为了解决跨域问题,需要实现跨域,很简单,实现一个filter即可。

    @Component
    public class SimpleCORSFilter 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");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
            chain.doFilter(req, res);
        }
    
        public void init(FilterConfig filterConfig) {
    
        }
    
        public void destroy() {
    
        }
    
    }
    

    相关文章

      网友评论

        本文标题:Springboot接口开启跨域访问

        本文链接:https://www.haomeiwen.com/subject/zkycuxtx.html