后端Java
第一步:CORSFilter.java
文件 过滤器
package com.seed.cayenne.console.web.app.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by lldzy on 2017/06/27.
*/
public class CORSFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
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, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy() {
}
}
第二步:webapp > WEB-INFO > web.xml
<!-- 解决跨域请求的filter -->
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>com.superyz.jcode.filter.CORSFilter</filter-class> //路径要改为上面Java文件的路径
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
网友评论