package cn.superdesk.uniorder.config.security;

import org.springframework.stereotype.Component;

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


/**
 * @Description: 跨域
 * @Author: linjinyu
 * @CreateDate: 5/21/20 10:28 AM
 * @UpdateUser: linjinyu
 * @UpdateDate: 5/21/20 10:28 AM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Component
public class CrossDomainFilter implements Filter {


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

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;

		// 设置允许跨域访问的地址
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
		response.setHeader("Access-Control-Allow-Methods", "HEAD, POST, GET, OPTIONS, DELETE, PUT");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type,Content-Disposition,Access-Token,token");

		// 是否支持cookie跨域
		response.setHeader("Access-Control-Allow-Credentials", "true");

		filterChain.doFilter(servletRequest, servletResponse);
	}

	@Override
	public void destroy() {

	}
}
