A filter (the Filter) Introduction
public interface the Filter { / ** Called when initialized filter * / public void the init (the FilterConfig FilterConfig) throws ServletException; / ** performs filter processing logic * / public void the doFilter (the ServletRequest Request, the ServletResponse Response, the FilterChain catena alberghiera) throws IOException, ServletException; / ** Called when destroyed filter * / public void the destroy (); }
Wherein the init method will be called during the initialization Filter, destroy method is called when the Filter destroyed while doFilter method is a method performed during operation of the filter, the need to protect the at least two logical doFilter, a filter is currently in need of treatment filtering logic, it is a jump to the next filter.
Custom filters as follows:
public class LogFilter the implements the Filter { / ** Called when the filter initialization * / public void the init (the FilterConfig FilterConfig) throws ServletException { System.out.println ( "Filter initialization" ); } public void the doFilter (the ServletRequest Request, the ServletResponse Response, catena alberghiera the FilterChain) throws IOException, ServletException { System.out.println ( "logic performs filtering" ); // jump to filter a link to the next link the chain.doFilter (Request, Response); } public void the destroy ( ) { System.out.println ( "Destruction Filter" ); } }
@Bean public FilterRegistrationBean initLogFilter () { FilterRegistrationBean the bean = new new FilterRegistrationBean (); LogFilter filter = new new LogFilter (); bean.setFilter (filter); // needs to be filtered URL path List <String> URLs = new new the ArrayList <String> ( ); urls.add ( "/ manage / *"); // only filter / manage / * path bean.setUrlPatterns (URLs); return the bean; } @Bean public FilterRegistrationBean initTimeFilter () { FilterRegistrationBean the bean = new newFilterRegistrationBean (); TimeFilter filter = new new TimeFilter (); bean.setFilter (filter); // needs to be filtered URL path List <String> URLs = new new the ArrayList <String> (); urls.add ( "/ *"); // filter all paths bean.setUrlPatterns (URLs); return the bean; }
@WebFilter(urlPatterns = "/*") @Order(1) public class LogFilter implements Filter { //............... }
@WebFilter(urlPatterns = "/manage/*") @Order(2) public class TimeFilter implements Filter { //...... }
@SpringBootApplication @ServletComponentScan public class Bootstrap { public static void main( String[] args ) { SpringApplication.run(Bootstrap.class); } }
Third, the realization of the principle Filter
Filter the same and Servlet containers are achieved through the Web, Web containers were stored, two of the Map Servlet and Filter, key path is disposed corresponding to url-parttern, value and each is an instance of Filter Servlet
When starting the web container will initialize all Filter, Filter and all the stored url Map, each time when a client request, the web container will get the request first, and then through all Filter, if satisfying Filter condition of the URL, will be added to the filter filter array.
Then all the filters in the filter array consisting of the filter chain, one by one logic executor filters.