Analysis of spring boot using filter procedure


This article mainly introduces the process analysis of spring boot using filter. The example code in this article is very detailed. It has a certain reference value for your study or work. You can refer to the following for your friends

First of all, let’s talk about the filter. The filter is the process of data filtering and preprocessing. When we visit the website, sometimes we will publish some sensitive information, and some of them will be replaced by * after we send it, and some of them will be controlled by login authority. A resource, without authorization, can’t be accessed by users at will. At this time, we can also use the filter. There are many other functions of the filter, such as implementing URL level permission control, compressing response information, encoding format, and so on.

The filter depends on the servlet container. Based on function callback, almost all requests can be filtered. Let’s talk about how to add filters in spring boot.

1. Introducing spring boot starter Web

The spring boot starter web package is introduced into pom.xml.


2. Establish filter program

@Order (1): represents the order of filters. Suppose we have multiple filters, how do you determine the execution order of filters? This annotation specifies the order of filters.

@Webfilter: indicates that this class is a filter.

For the parameters in the filter, filtername is the filter name, urlpatterns is the filter range, and initparams is the filter initialization parameter.

Three ways to filter

Init: the filter object is created only once, and the init method is executed only once.

Dofilter: the main business code writing method, which can be repeatedly called

Destroy: called automatically when the filter is destroyed (the program closes or actively destroys the filter).

@WebFilter(filterName = "piceaFilter", urlPatterns = "/*" , initParams = {
    @WebInitParam(name = "URL", value = "http://localhost:8080")})
public class PiceaFilter implements Filter {

  private String url;
   *You can initialize the initialization parameters configured by filter in web.xml
   *The filter object is created only once, and the init method is executed only once.
   * @param filterConfig
   * @throws ServletException
  public void init(FilterConfig filterConfig) throws ServletException {
    this.url = filterConfig.getInitParameter("URL");
    System. Out. Println ("I'm the initialization method for filters! Url = "+ this. URL +", life begins... ");

   *Main business coding methods
   * @param servletRequest
   * @param servletResponse
   * @param filterChain
   * @throws IOException
   * @throws ServletException
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    System. Out. Println ("I am the execution method of the filter, and the client's request to the servlet is blocked by me");
    filterChain.doFilter(servletRequest, servletResponse);
    System. Out. Println ("I am the execution method of the filter, and the response sent by the servlet to the client is intercepted by me");

   *Called automatically when the filter is destroyed.
  public void destroy() {
    System. Out. Println ("I am the method called when the filter is destroyed! ");

3. Creating the contour class

This class is relatively simple, no special instructions

public class PiceaContoller {

  public void asyncTask() throws Exception {
    System. Out. Println ("I'm the method in the control class, I'm thinking about...");

4. Add comments to startup class and register filter automatically

@Servletcomponentscan: after @ servletcomponentscan annotation is used on springbootapplication, servlet, filter and listener can be registered automatically through @ webservlet, @ webfilter, @ weblistener annotation directly without any other code.

public class SpringBootFiFilterApplication {

  public static void main(String[] args) {, args);


5. Test results

Well, it’s time to see the effect again.

Enter: http: / / localhost: 2001 / query in browse

At this time, the input of the console is as follows.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today Application of regular expression

1. Balanced group / recursive matching (?’ Group ‘), which is called the corresponding content of group, and counts it on the stack;(?’- Group ‘), and count the corresponding content named group out of the stack(?!) Zero width negative look ahead assertion. Since there is no suffix expression, attempts to match always failRegular example:,{0,1}”5″:\[[^\[\]]*(((?’Open’\[)[^\[\]]*)+((?’-Open’\])[^\[\]]*)+)*(?(Open)(?!))\],{0,1} Test […]