1. Complete the registration of the Filter component by scanning the annotations
Create a class, implement the Filter interface, and implement the doFilter() method
Use annotation @WebFilter in this class , set filterName and urlPatterns
Write code in doFilter
Write startup class: add annotation @ServletComponentScan
/** * SpringBoot Integration Filter Method One Project www.fhadmin.org */ //@WebFilter(filterName="FirstFilter" , urlPatterns= {"*.do","*.jsp"}) @WebFilter(filterName="FirstFilter" , urlPatterns= "/first") public class FirstFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("进入Filter"); chain.doFilter(request, response); System.out.println("Leave Filter"); } }
//项目 www.fhadmin.org
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
urlPatterns is an array type, which can intercept multiple For example: urlPatterns= {" .do"," .jsp"}
2. Complete the Filter component registration through the method
Create a class, implement the Filter interface, and implement the doFilter() method
Write startup class
Add a method, the return value must be an object of FilterRegistrationBean , he can create an instance of the Filter object
Create a FilterRegistrationBean object and pass in the instantiated Filter object
Add Url, bean.addUrlPatterns();
Return FilterRegistrationBean object
Add @Bean annotation to this method
/** * Register for the Filter project www.fhadmin.org */ @Bean public FilterRegistrationBean getFilterRegistrationBean() { FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); //bean.addUrlPatterns(new String[] {"*.do","*.jsp"}); bean.addUrlPatterns("/second"); return bean; }