Springboot使用Filter以及踩过的坑
在Springboot中使用Filter有两种方式,注解方式,注册bean方式
一、注解@WebFilter
1、实现Filter接口(javax.servlet)
2、添加@WebFilter注解
3、启动类添加@ServletComponentScan注解
附上代码:
第一个Filter:
@Slf4j @WebFilter(filterName = "filter1", urlPatterns = {"/url1/*"}) public class Filter1_Filter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("do filter1"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }