Filter seven: filter chain

table of Contents

One: Introduction to the filter chain

2: Filter chain case

Three: The first issue that needs attention: the relationship between the writing position of chain.dofilter() and the execution in the request and response process

Supplement: control filter chain interruption: blocking requests (a simple firewall)

Four: The second issue that needs attention: the order in which the filters are executed in the annotation form (just for understanding, because this strategy is basically not used in actual work)


One: Introduction to the filter chain

As shown in the figure below, filter 1, filter 2, and Servlet form a complete filter chain.

Note: When chain.doFilter() is passed backward, it is based on the writing order of <filter-mapping> in web.xml;


2: Filter chain case

Case: Including, FilterA, FilterB, FilterC three filters, web.xml configuration file, HelloServlet this Servlet:

web.xml: conforming configuration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>filter-chain</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>filterAA</filter-name>
  	<filter-class>com.imooc.filter.FilterA</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>filterAA</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <filter>
  	<filter-name>filterBB</filter-name>
  	<filter-class>com.imooc.filter.FilterB</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>filterBB</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <filter>
  	<filter-name>filterCC</filter-name>
  	<filter-class>com.imooc.filter.FilterC</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>filterCC</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

HelloServlet.java: This Servlet is not the focus of this blog

package com.imooc.filter;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloServlet
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().println("hello world!");
		System.out.println("hello nihao!!!");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

After starting the application, visit localhost:8080/filter-chain/hello: the browser page can display the content returned by HelloServlet, this is very simple;

At the same time, in the console: According to the writing order of <filter-mapping> in web.xml, execute the three filters in sequence; when the execution reaches the filter C, there are no filters next, and the request is passed to HelloServlet ;


Three: The first issue that needs attention: the relationship between the writing position of chain.dofilter() and the execution in the request and response process

But if you write the chain.doFilter() of the three filters FilterA, FilterB, and FilterC in front:

Start the application at this time and visit localhost:8080/filter-chain/hello:

Reason explanation: In the doFilter() method of FilterA, FilterB, and FilterC, chain.doFilter() is put in front, which means that when the request comes, this filter will execute chain.doFilter( ) Method, when the statement chain.doFilter() is executed, the request will be passed to the next filter, so that from FilterA to FilterB, from FilterB to FilterC, the console will print A in turn! , B! And C! ; It has been passed to HelloServlet and output "hello nihao!!!";

Then, when responding, in the opposite direction, execute the content after the chain.doFilter() statement in the doFilter() method in FilterC , then FilterB, and then FilterA; in this way, the control tower will print out This in turn. is Filter C! ;This is Filter B! and This is Filter A!; ( Note, this is only ok here, and it has not been verified many times, but it should be no problem with a high probability~~~ )


Supplement: control filter chain interruption: blocking requests (a simple firewall)

Start the application:

This kind of scenario where the chain.doFilter() method call is deliberately controlled in the program is relatively common; for example: Internet security is very important. For example, the developed website is only developed for Chinese IP, and foreign IPs cannot be accessed. What should I do? Huh?

The requested IP can be obtained. If the IP is Chinese, call the chain.doFilter() method to release the request; if the IP is a foreign country, pass the if else judgment and do not execute the sentence chain.doFilter(). Interrupt the request , so that the request whose IP is a foreign country is blocked by the filter; (this is also a simple firewall)


Four: The second issue that needs attention: the order in which the filters are executed in the annotation form (just for understanding, because this strategy is basically not used in actual work)

Regarding the execution order of the filter: (1) When the filter is configured in web.xml, it is executed in sequence according to the writing order of <filter-mapping> in web.xml; (2) The filter is annotated At that time, how to determine the execution order of filters? ? ?

as follows:

Start the application and visit localhost:8080/filter-chain/hello:

In fact, when the filter is in the form of annotations: it determines the order according to the class name, in ascending alphabetical order, from small to large, and the letters are not case sensitive; therefore, first execute FilterA, then execute FilterB, and finally execute FilterC;

For example, when the class name of the FilterA filter class is changed to FilterZ: when visiting again:

 

Guess you like

Origin blog.csdn.net/csucsgoat/article/details/114289581