package com.ctrip.zeus.crossdomain;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by lu.wang on 2016/6/16.
 */
public class CrossDomainFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        final HttpServletResponse response = (HttpServletResponse) servletResponse;

        String origin = request.getHeader(HeaderUtil.ORIGIN);
        if (origin != null) {
            // Client request header contains the origin header
            response.setHeader(HeaderUtil.ALLOW_ORIGIN, origin);
            String requestMethod = request.getHeader(HeaderUtil.REQUEST_METHOD);
            String requestHeaders = request.getHeader(HeaderUtil.REQUEST_HEADERS);
            if (request.getMethod().equals(HeaderUtil.OPTIONS_METHOD) && (requestMethod != null || requestHeaders != null)) {
                response.setHeader(HeaderUtil.ALLOW_HEADERS, requestHeaders);
                response.setHeader(HeaderUtil.ALLOW_METHODS, requestMethod);
                response.setHeader(HeaderUtil.ALLOW_CREDENTIALS, HeaderUtil.HeaderValues.ALLOW_CREDENTIALS_TRUE);
            }
        }
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}

@@ -938,16 +938,127 @@
 rigin);%0A
+            response.setHeader(HeaderUtil.ALLOW_CREDENTIALS, HeaderUtil.HeaderValues.ALLOW_CREDENTIALS_TRUE);%0A%0A
         
@@ -1498,122 +1498,8 @@
 d);%0A
-                response.setHeader(HeaderUtil.ALLOW_CREDENTIALS, HeaderUtil.HeaderValues.ALLOW_CREDENTIALS_TRUE);%0A
     
@@ -1555,16 +1555,17 @@
 request,
+ 
 response

fix cross domain error