This is the second day of my participation in the August Text Challenge.More challenges in August

Introduction to the

As one of the three Java tools, the Filter has a high status in the use of Java Web. Filters are server-side programs that implement the Javax.servlet.filter interface.

Filter has the following functions:

  • Intercepts the client’s HttpServletRequest until it reaches the Servlet.
  • Check the HttpServletRequest as needed, and modify the HttpServletRequest header and data as well.
  • Intercepting HttpServletResponse before it reaches the client.
  • Check the HttpServletResponse as needed, and modify the HttpServletResponse headers and data as well.

Filter has the following methods:

  • Void init(FilterConfig FilterConfig) is used to initialize the filter.
  • Void doFilter(ServletRequest Request, ServletResponse Response,FilterChain chain) implements filtering. This method adds additional processing to each request.
  • Void destroy() is used to reclaim certain resources before the filter is destroyed.

This is implemented via @webFilter

You can use @WebFilter and @ServletComponentScan to implement Filter. Where, @webFilter annotates the Filter class of the Filter interface and annotates the corresponding parameters; @ServletComponentScan annotation in the SpringBoot startup class, After annotation, servlets, filters, and listeners can be automatically registered via @webServlet, @webFilter, and @WebListener annotations without additional code!

The specific code is as follows:

TestFilter.java

package com.example.demo.filter; import org.springframework.core.annotation.Order; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import java.io.IOException; /** * @description: * @time: 2021/8/3 16:12 */ @WebFilter(initParams = {@WebInitParam(name="testFilter",value = "/test")}) @Order(1) public class TestFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); System.out.println("-----Filter initialization completed -----"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain FilterChain) throws IOException, ServletException {system.out. println("----- execute the code before invoking the service -----"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("----- execute code after calling service -----"); } @Override public void destroy() { Filter.super.destroy(); System.out.println("Filter destroyed "); }}Copy the code

TestController.java

package com.example.demo.controller; import com.example.demo.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * @description: * @time: 2021/8/3 16:04 */ @RestController public class TestController { @Autowired TestService testService; @RequestMapping(value = "/test",method = RequestMethod.GET) public Object test(){ testService.printTest(); return "test"; }}Copy the code

Demo1Application.java

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class Demo1Application { public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); }}Copy the code

The running results are as follows:

Implemented via @bean

Here, to test the @Order effect, two filter tests are used.

WebConfig.java

package com.example.demo.config; import com.example.demo.filter.FirstFilter; import com.example.demo.filter.SecondFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.Filter; /** * @description: * @time: 2021/8/4 10:11 */ @Configuration public class WebConfig { @Bean public FilterRegistrationBean firstFilterRegistrationBean(){ FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>(); filterFilterRegistrationBean.addUrlPatterns("/hello"); / / set the priority filterFilterRegistrationBean. SetOrder (1); FirstFilter filter = new FirstFilter(); filterFilterRegistrationBean.setFilter(filter); return filterFilterRegistrationBean; } @Bean public FilterRegistrationBean secondFilterRegistrationBean(){ FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>(); filterFilterRegistrationBean.addUrlPatterns("/hello"); / / set the priority filterFilterRegistrationBean setOrder (2); // Bind filter SecondFilter = new SecondFilter(); filterFilterRegistrationBean.setFilter(filter); return filterFilterRegistrationBean; }}Copy the code

FirstFilter.java

package com.example.demo.filter; import javax.servlet.*; import java.io.IOException; /** * @description: * @time: 2021/8/4 10:09 */ public class FirstFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); System. The out. Println (" -- -- -- -- -- FirstFilter initialization is complete -- -- -- -- -- "); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain FilterChain) throws IOException, ServletException {system.out. println("----- execute code FirstFilter----- before service "); filterChain.doFilter(servletRequest,servletResponse); System. Out. Println (" -- -- -- -- -- service after executing code FirstFilter -- -- -- -- -- "); } @Override public void destroy() { Filter.super.destroy(); System.out.println("FirstFilter destroyed "); }}Copy the code

SecondFilter.java

package com.example.demo.filter; import javax.servlet.*; import java.io.IOException; /** * @description: * @time: 2021/8/4 10:30 */ public class SecondFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); System. The out. Println (" -- -- -- -- -- SecondFilter initialization is complete -- -- -- -- -- "); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain FilterChain) throws IOException, ServletException {system.out. println("----- execute code SecondFilter----- before serving "); filterChain.doFilter(servletRequest,servletResponse); System. Out. Println (" -- -- -- -- -- service after executing code SecondFilter -- -- -- -- -- "); } @Override public void destroy() { Filter.super.destroy(); System.out.println("SecondFilter destroyed "); }}Copy the code

The execution effect is as follows:

As you can see from the results, the initialization of the Filter service is completed when the Spring Boot project is started. When the user accesses the service, the order of execution is based on the priority of order (the smaller the number, the higher the priority).