filter类
package com.xiaoping.house.filter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.*; import java.io.IOException; public class LogFilter implements Filter { private Logger logger= LoggerFactory.getLogger(getClass()); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { logger.info("Request--comming"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
注册filter
package com.xiaoping.house.filter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; @Configuration public class FilterBeanConfig { @Bean public FilterRegistrationBean logFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new LogFilter()); ArrayList<String> urlList = new ArrayList<>(); urlList.add("*"); filterRegistrationBean.setUrlPatterns(urlList); return filterRegistrationBean; } }