package com.hzya.frame.webapp.web.filter;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.util.List;

/**
 * @Content
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-05 9:01
 * @Version 1.0
 */
public class BaseFilter implements Filter {
    static final String TOKEN = "20220423344556abac";
    //内部接口集合
    public static List<String> INSIDE_URLS = Lists.newArrayList("/index", "/inside");
    //白名单接口集合
    public static List<String> WHITE_PATH = Lists.newArrayList("/white", "/login");

    //无需认证接口
    public static List<String> NotCertifiedService = Lists.newArrayList("seeYonInterFace", "ddInterfaceService");

    Logger logger = LoggerFactory.getLogger(BaseFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("初始化数据");
    }

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

        //logger.info("第二过滤器：验证权限");
        HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse);
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        String service = request.getHeader("as");
        String serviceMethod = request.getHeader("dj");
        //要访问的Bean名称
        String tl = request.getHeader("tl");

        if (INSIDE_URLS.contains(requestURI)) {
            //内部接口，直接通过
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (WHITE_PATH.contains(requestURI)) {
            //白名单接口，直接通过
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (NotCertifiedService.contains(tl)) {
            //白名单接口，直接通过
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        //校验请求的服务是否有存在权限
        //if (ObjectUtils.isEmpty(serviceMethod)) {
        //    logger.info("当前请求的服务为空：" + service);
        //    throw new BaseSystemException("当前请求的方法为空！");
        //} else {
        //    logger.info("当前请求的方法：" + service);
        //}
        //
        //if (ObjectUtils.isEmpty(service)) {
        //    logger.info("当前请求的服务为空：" + service);
        //    throw new BaseSystemException("当前请求的服务为空！");
        //} else {
        //    logger.info("当前请求的服务：" + service);
        //}


        //进行校验，如token校验
        String token = request.getHeader("token");
        if (TOKEN.equals(token)) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            //token校验不通过，重定向到登录页面


//
            filterChain.doFilter(servletRequest, servletResponse);
//            wrapper.sendRedirect("/login");
        }

    }

    @Override
    public void destroy() {
    }
}

