spring-mvc 解决跨域问题

解决跨域访问问题,只需在被访问的应用中加入一个请求过滤器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public 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 {
}
}

------本文结束  感谢阅读------