java后端解决跨域访问

参考文章:http://blog.csdn.net/puma_dong/article/details/51395976

1.在web.xml中定义跨域处理器Filter

 <filter>  
      <filter-name>corsfilter-name>  
      <filter-class>com.package.SimpleCORSFilterfilter-class>  
    filter>  
    <filter-mapping>  
      <filter-name>corsfilter-name>  
      <url-pattern>/*url-pattern>  
    filter-mapping> 

2.自定义Fileter的实现


public class SimpleCORSFilter implements Filter{


@Override
public void destroy() {
// TODO Auto-generated method stub

}


@Override
public void doFilter(ServletRequest request, ServletResponse response,  
                         FilterChain chain) throws IOException, ServletException {  
        try {  
            HttpServletRequest httpRequest = (HttpServletRequest) request;  
            HttpServletResponse httpResponse = (HttpServletResponse) response;  
  
            // 跨域  
            String origin = httpRequest.getHeader("Origin");  
            if (origin == null) {  
                httpResponse.setHeader("Access-Control-Allow-Origin", "*");  
            } else {  
                httpResponse.setHeader("Access-Control-Allow-Origin", origin);  
            }  
            httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, x-requested-with, Content-Type, Accept,X-Cookie");  
            httpResponse.setHeader("Access-Control-Allow-Credentials", "true");  
            httpResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS,DELETE");  
            if ( httpRequest.getMethod().equals("OPTIONS") ) {  
                httpResponse.setStatus(HttpServletResponse.SC_OK);  
                return;  
            }  
            chain.doFilter(request, response);  
        } catch (Exception e) {  
            throw e;  
        }  
    }  


	@Override
	public void init(FilterConfig arg0) throws ServletException {
	// TODO Auto-generated method stub
	}
}

你可能感兴趣的:(java后端解决跨域访问)