过滤器比较简单,通过注解方式进行配置
@Component
@WebFilter(urlPatterns = "/*",filterName = "loggerFilter")
public class LoggerFilter implements Filter {
........
}
拦截器则是需要多一步骤,先是需要配置拦截器类
@Component
public class TestInterceptor implements HandlerInterceptor{
//逻辑代码
}
同时还需要一个adapter类,配置拦截器的配置信息
@SuppressWarnings("deprecation")
@SpringBootConfiguration
public class WebMvcConfigurerAdapter extends org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter {
@Autowired
private RequestInterceptor requestInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
registry.addInterceptor(testInterceptor).addPathPatterns("/**");
}
}
至此基本的拦截器就完成了