Using Filter in SpringBoot

Keywords: Programming SpringBoot Spring

1. Add filter to traditional web project

<filter>
   <filter-name>TestFilter</filter-name> <!--Definition filter Name and filter class -->
    <filter-class>com.jiafeng.filter.TestFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>TestFilter</filter-name> 
  <url-pattern>/*</url-pattern>  <!--Set up filter Interception path -->
  <init-param>
    <param-name>paramName</param-name>  <!-- Add initial parameters -->
    <param-value>paramValue</param-value>
  </init-param>
</filter-mapping>

2. There are two ways to add a filter to springboot

2.1 code registration method

First define the filter class: Myfilter, and then register the filter into the call chain through the FilterRegistrationBean.


@Configuration   //Equivalent to the < beans > tag in spring
public class WebConfiguration {

	@Bean   //Equivalent to < bean > tag in spring
	public FilterRegistrationBean<MyFilter> testFilterRegistration() {
		FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>();
		registration.setFilter(new MyFilter());
		registration.addUrlPatterns("/*");//Configure filter path
		registration.addInitParameter("paramName", "paramValue");//Add initial value
		registration.setName("myFilter");//Set filter name
		registration.setOrder(1);//The order of filter execution in the request. The smaller the value, the earlier the execution
		return registration;
	}

	public class MyFilter implements Filter {

		@Override
		public void destroy() {
			System.out.println("Destruction MyFilter");
		}

		@Override
		public void doFilter(ServletRequest sRequest, ServletResponse sResponse, FilterChain chain)
				throws IOException, ServletException {
			HttpServletRequest request = (HttpServletRequest) sRequest;
			System.out.println("this is MyFilter url :" + request.getRequestURI());
			chain.doFilter(sRequest, sResponse);
		}

		@Override
		public void init(FilterConfig arg0) throws ServletException {
			System.out.println("Initialization MyFilter");
		}

	}
}

2.2 annotation registration method

Define filter class: HelloFilter, add @ WebFilter annotation to filter class to configure filter name, filter path and other attributes, and configure filter execution Order through @ Order.

Note that when using servlet annotations such as @ WebServlet, @ WebFilter, @ WebListener, etc., you need to add @ ServletComponentScan annotation on the startup class of springboot, otherwise it will not take effect.

@WebFilter(urlPatterns="/*",filterName="helloFilter",initParams= {@WebInitParam(name="paramName",value="paramValue"),@WebInitParam(name="paramName2",value="paramValue2")})
@Order(2)
public class HelloFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
			throws IOException, ServletException {
		System.out.println("Execution filter Hello Filter !");
		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("Initialization HelloFilter!");
	}

}

Startup class

@SpringBootApplication
@ServletComponentScan
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

Posted by therainmaker53 on Mon, 04 Nov 2019 12:14:39 -0800