Road to a Java Web: filter (Filter)

A filter (the Filter) Introduction

Filter is a web resource to intercept and do some processing and then to the next filter or Servlet processing, the main can intercept request and response
The filter in the form of an assembly to bind with the web, using a chain to work.
 
Benefits filters:
Can intercept requests and responses, in order to view or retrieve interaction data between the client and the server, to realize the function of filtering. Further filters can be dynamically added or deleted without need to modify the web application logic.
 
Filter initialization and destruction are achieved through the Web container, Web container will be executed after the init method to initialize Filter object performs destory method before destroying Filter object.
 
Second, the use of the filter
2.1, Filter basic use
Filter the Servlet interface is defined as follows:
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" ); 
    } 
}

 

In this paper SpringBoot embodiment, Filter class configuration as follows:
    @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; 
    } 
Custom filters need to implement the Filter interface, override three methods Filter interface, you can then configure the Filter will perform the init method when the project starts, it will destory the implementation of the method when the project is closed, each time the client initiates a request will be executed once doFilter method, the doFilter chain.doFilter must perform the method forwards the request to the next filter,
Otherwise, the request can not reach the final layer of the business logic, the same configuration and perform the execution sequence of a plurality Filter, on the first execution of the first configuration may be provided in a different execution order Filter manually, directly in theory each is independently Filter of. 
 
2.2, Filter advanced use
Filter different path may need to filter the actual case is not the same, different paths may be filtered by setting different Filter, the above embodiment only LogFilter interception request / manage / * path, and TimeFilter will filter all requests;
Another addition to the filtered path, may be provided different Filter manually executed in different order, you may be set by @Order annotation, but also provides Spring @WebFilter Filter annotation may be directly defined, and then without a separate annotation defined @Bean It is used as follows:
@WebFilter(urlPatterns = "/*")
@Order(1)
public class LogFilter implements Filter {
   
    //...............

}
@WebFilter(urlPatterns = "/manage/*")
@Order(2)
public class TimeFilter implements Filter {

   //......
 
}
Defines two Filter, wherein LogFilter first execution order, and filter all requests; TimeFilter second execution order, the filter / manage / * path of the request
And annotations are @WebFilter Servlet annotation, it is necessary to add annotations @ServletComponentScan Servlet scanning start annotation class, shown below:
@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.

 
 
 
 
 
 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/jackion5/p/11990858.html