spring-mvc 解决跨域问题 发表于 2016-01-19 | 分类于 技术 | | 阅读次数 字数统计 138 字 | 阅读时长 1 分钟 解决跨域访问问题,只需在被访问的应用中加入一个请求过滤器: 12345678910111213141516171819202122232425262728public class CorsFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; if (req.getHeader("Origin") != null) { res.addHeader("Access-Control-Allow-Origin", "*"); } if ("OPTIONS".equals(req.getMethod())) { res.addHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST"); res.addHeader("Access-Control-Allow-Headers","Origin, Content-Type"); res.addHeader("Access-Control-Max-Age", "-1"); } chain.doFilter(req, res); } @Override public void destroy() { } @Override public void init(FilterConfig filterConfig) throws ServletException { }} ------本文结束 感谢阅读------ 本文作者: cipher 本文链接: http://www.ciphermagic.cn/spring-mvc-cors.html 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 CN 许可协议。转载请注明出处!