package com.tzld.piaoquan.api.filter;

import com.tzld.piaoquan.api.aop.LogRequestAop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.UUID;

/**
 * auth filter
 *
 * @author ehlxr
 */
@Order(value = 1)
@WebFilter(filterName = "customFilter", urlPatterns = {"/*"})
public class CustomFilter implements Filter {
    private static final Logger log = LoggerFactory.getLogger(CustomFilter.class);

    @Override
    public void init(FilterConfig filterConfig) {
        log.info("customFilter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        MDC.put(LogRequestAop.LOG_TRACE_ID, UUID.randomUUID().toString().replaceAll("-", ""));
        filterChain.doFilter(servletRequest, servletResponse);
        MDC.remove(LogRequestAop.LOG_TRACE_ID);
    }

    @Override
    public void destroy() {
        log.info("customFilter destroy");
    }
}
