springboot工程中使用Filter过滤器与其他地方使用基本相同,只是注入的方式不同。
实现Filter接口,重写doFilter方法
filterChain.doFilter(servletRequest,servletResponse);表示放行
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("=======过滤器执行========");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
@Configuration
public class MyServerConfig {
//注册三大组件--Filter
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean filter=new FilterRegistrationBean();
filter.setFilter(new MyFilter());
filter.setUrlPatterns(Arrays.asList("/getUser","/myservlet"));
return filter;
}
}