package egovframework.com.its.web.server.interceptors;

import egovframework.com.its.web.server.util.IpCheck;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.UnknownHostException;

@Slf4j
@Component
public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws UnknownHostException {
        String possibleIp = "172.19.";
//        String possibleIp = "0:0:0:0:0:0:0:1";
        if (IpCheck.getClientIp(request).contains(possibleIp)) {
            return true;
        }
        try {
            log.error("관리자 페이지 접근 불가 IP 입니다. IP를 확인해주세요. IP : {}", IpCheck.getClientIp(request));
            response.sendRedirect("/");
        } catch (IOException e) {
            log.error("AdminInterceptor: preHandle... 잘못된 페이지 입니다.");
        }

        return false;
    }
}
