在web.xml 中的配置
<filter> <filter-name>crossorigin</filter-name> <filter-class>com.utils.CORSFilter</filter-class> </filter> <filter-mapping> <filter-name>crossorigin</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在com.utils下以下
/** * */ package com.utils; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; /** * @author jomin * */ public class CORSFilter implements Filter { public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletResponse response = (HttpServletResponse) servletResponse; 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"); filterChain.doFilter(servletRequest, servletResponse); } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
--------------------------------------------
转自:https://blog.csdn.net/zpf0918/article/details/52624813