package egovframework.config;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("Start Method checking");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
        res.setHeader("Pragma", "no-cache");
        res.setHeader("Content-Security-Policy",
                "default-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net http://dapi.kakao.com https://dapi.kakao.com http://*.daumcdn.net https://*.daumcdn.net; "+
                "script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net http://dapi.kakao.com https://dapi.kakao.com http://*.daumcdn.net https://*.daumcdn.net; " +
                "style-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net http://dapi.kakao.com https://dapi.kakao.com http://*.daumcdn.net https://*.daumcdn.net; "+
                "img-src 'self' data: https: 'unsafe-inline' 'unsafe-eval' http://dapi.kakao.com https://cdn.jsdelivr.net https://dapi.kakao.com http://*.daumcdn.net https://*.daumcdn.net;");
        res.setHeader("Strict-Transport-Security", "max-age=31536000");
        res.setHeader("X-Content-Type-Options", "nosniff");
        res.setHeader("X-Frame-Options", "SAMEORIGIN");
        res.setHeader("X-Xss-Protection", "1; mode=block");
        res.setHeader("Access-Control-Allow-Methods", "GET, POST");

        req.setCharacterEncoding("UTF-8");

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        log.info("End Method checking");
    }
}
