How to integrate Filter with SpringBoot
This article will explain in detail how SpringBoot integrates Filter. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
SpringBoot integrates filter 1, creates a basic SpringBoot project, and integrates Web to org.springframework.bootspring-boot-starter-web 2. Method 1: inject 1. Create a Filter component
Com.zhl.springbootweb.filter.FirstFilter
UrlPatterns can specify a suffix or URL
Implement @ WebFilter annotations
Integrate Filter in javax.servlet
Package com.zhl.springbootweb.filter;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import java.io.IOException / * integrate Filter mode: * / / @ WebFilter (filterName = "first Filter", urlPatterns = {"* .do", "* .jsp"}) @ WebFilter (filterName = "first Filter", urlPatterns = {"/ first"}) public class FirstFilter implements Filter {@ Override public void init (FilterConfig filterConfig) throws ServletException {} @ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println ("enter FirstFilter") / * release * / filterChain.doFilter (servletRequest,servletResponse); System.out.println ("leave First Filter");} @ Override public void destroy () {}} 2. The startup class needs to be annotated @ ServletComponentScan@SpringBootApplication/* scans @ WebServlet,@WebFilter annotation when SpringBoot starts, and instantiates * / @ ServletComponentScanpublic class SpringbootWebApplication {public static void main (String [] args) {SpringApplication.run (SpringbootWebApplication.class, args);}} 3. test
Visit localhost:8080/first
Enter First FilterFirst Servlet and leave First Filter 3. Mode 2: use method injection 1. Create Filter
Com.zhl.springbootweb.filter.SecondFilter
Package com.zhl.springbootweb.filter;import javax.servlet.*;import java.io.IOException;/** Integration Filter 2 * * / public class SecondFilter implements Filter {@ Override public void init (FilterConfig filterConfig) throws ServletException {} @ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println ("enter SecondFilter"); / * release * / filterChain.doFilter (servletRequest,servletResponse) System.out.println ("leave SecondFilter");} @ Override public void destroy () {}} 2. Create a configuration class / * Filter configuration class * / @ Configurationpublic class FilterConfig {@ Bean public FilterRegistrationBean getFilterRegistrationBean () {FilterRegistrationBean bean = new FilterRegistrationBean (new SecondFilter ()); / / bean.addUrlPatterns (new String [] {"* .do", "* .jsp"}); bean.addUrlPatterns ("/ second"); return bean;} 3. test
Visit http://localhost:8080/second
Enter SecondFilterSecond Servlet and leave SecondFilter to share here on how SpringBoot integrates Filter. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.