springboot配置 java servlet的Filter过滤器


import java.io.IOException;

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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
@WebFilter(filterName="myFilter",urlPatterns="/*")
//WebFilter注解会让springboot知道这是个过滤器,会跟随boot启动urlPatterns就是要过滤的路径,filterName过滤器名字
public class ActionFilter implements Filter{
     private HttpServletRequest httpRequest;
     private HttpServletResponse httpResponse;
     protected final Logger logger = Logger.getLogger(this.getClass());
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

         System.out.println("过滤器初始化");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("执行过滤操作");
        httpRequest=(HttpServletRequest)request;
        httpResponse=(HttpServletResponse)response;
        logger.info("url "+httpRequest.getRequestURI());
        // TODO Auto-generated method stub
        chain.doFilter(request, response);

    }

    @Override
    public void destroy() {
         System.out.println("过滤器销毁");
        // TODO Auto-generated method stub

    }

}

你可能感兴趣的:(java,springboot,servlet,filter,java,springboot)