package com.example.demo.config;

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

//@WebFilter("/*")
public class CrosFilter 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", "http://47.99.112.2:8085");
        response.setHeader("Access-Control-Allow-Origin", "https://mgs.microbialgenomic.cn");

        //跨越时允许使用的请求方法
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,PUT,DELETE");
        //跨域时允许设置的头有哪些
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,Content-Type");
        //跨域时是否允许携带cookie的凭证，但是目前还不可以使用cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
