package com.winsun.item.core.intercept;

import com.winsun.auth.core.support.WafRequestWrapper;
import org.springframework.stereotype.Component;

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

/**
 *
 */
@Component
public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        WafRequestWrapper wafRequestWrapper = new WafRequestWrapper((HttpServletRequest) request);

        httpResponse.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
        httpResponse.setHeader("Access-Control-Allow-Origin", "https://dx.dianyuanjiangli.com");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*ytx.21cn.com");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*api.weixin.qq.com");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*mp.weixin.qq.com");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*open.weixin.qq.com");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*mp.weixin.qq.com");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*ismart.mini189.cn");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*gzctpay.mini189.cn");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*enter.gd189.cn");
        httpResponse.setHeader("Access-Control-Allow-Origin", "*icloud.mini189.cn");
        httpResponse.setHeader("Access-Control-Allow-Origin", "14.116.225.*");

        httpResponse.setHeader("Access-Control-Allow-Methods", wafRequestWrapper.getMethod());
        httpResponse.setHeader("Access-Control-Max-Age", "3600");
        httpResponse.setHeader("Access-Control-Allow-Headers", wafRequestWrapper.getHeader("Access-Control-Request-Headers"));
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
