Java Web实现跨域

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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpStatus;

public class CorsFilter 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;
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    // 指定允许其他域名访问
    response.setHeader("Access-Control-Allow-Origin", "*");
    // 响应类型
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, OPTIONS, DELETE");
    // 响应头设置
    response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header, HaiYi-Access-Token");
    if ("OPTIONS".equals(request.getMethod())){
      response.setStatus(HttpStatus.SC_NO_CONTENT);
    }
    filterChain.doFilter(servletRequest, servletResponse);
  }

  @Override
  public void destroy() { }
}
<filter>
    <filter-name>cors</filter-name>
    <filter-class>com.hy.fliter.CorsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cors</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

 

参考:

http://www.jb51.net/article/91794.htm(以上内容转自此篇文章)

posted @ 2017-12-27 09:16  EasonJim  阅读(635)  评论(0编辑  收藏  举报