在后台写个过滤器就好了
package com.springapp.mvc.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by TaoYuan on 2017/4/26 0026.
*/
public class CrossDomainFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type");
response.setHeader("Access-Control-Allow-Credentials", "true");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
-
web.xml设置通配
<filter> <filter-name>crossDomainFilter</filter-name> <filter-class>com.springapp.mvc.filter.CrossDomainFilter</filter-class> </filter> <filter-mapping> <filter-name>crossDomainFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>