package com.xtd.framework.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@Component
public class MyFilter implements Filter {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(" myfilter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        logger.info("******目标访问参数******");
        logger.info("目标访问路径:" + requestURI);
        logger.info("目标访问提交方式:" + method);
        Map<String, String[]> map = request.getParameterMap();
        map.forEach((k, v) -> {
            logger.info("请求参数-- " + k + ": " + v[0]);
        });
        String authorization = request.getHeader("Authorization");
        logger.info("密钥:" + authorization);
        logger.info("************************");
        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {
        System.out.println("myfilter destroy");
    }
}
