内网电脑访问不了另外一台电脑启动的服务器,跨域问题

服务器启动,前端同事访问不了,也检查防火墙,开了防火墙,还是访问不了,地址也没问题,后来发现是跨域问题。

解决方法:

public class SimpleCORSFilter implements Filter {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) arg1;  

            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");  

            log.error("req:"+JSONObject.toJSONString(arg0.getParameterMap())+":"+arg0.getLocalAddr()+":"
            +arg0.getLocalName()+":"+arg0.getServerName()+":"+arg0.getProtocol());
            arg2.doFilter(arg0, arg1); 
    }

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

    }

}

web.xml 加


<filter>  
      <filter-name>corsfilter-name>  
      <filter-class>com.session.SimpleCORSFilterfilter-class>  
filter>

你可能感兴趣的:(java)