Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to configure Filter filtering interception in SpringBoot

Shulou Source: shulou.com Published: 2022-06-02 06:59:18 09月18日 Update

This article will explain in detail how to configure Filter filtering and blocking in SpringBoot. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Complete the registration of Filter components by scanning comments

Create a class that implements the Filter interface and the doFilter () method

Use the annotation @ WebFilter in this class to set filterName and urlPatterns

Write code in doFilter

Write startup classes: add the annotation @ ServletComponentScan

/ * SpringBoot Integration Filter Project www.1b23.com * / @ WebFilter (filterName= "FirstFilter", urlPatterns= {"* .do", "* .jsp"}) @ WebFilter (filterName= "FirstFilter", urlPatterns= "/ first") public class FirstFilter implements Filter {@ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException ServletException {System.out.println ("enter Filter") Chain.doFilter (request, response); System.out.println ("leave Filter");}} / / Project www.1b23.com@SpringBootApplication@ServletComponentScanpublic class App {public static void main (String [] args) {SpringApplication.run (App.class, args);}}

UrlPatterns is of array type and can intercept multiple urlPatterns= {".do", ".jsp"}

two。 Complete the registration of Filter components through the method

Create a class that implements the Filter interface and the doFilter () method

Write startup classes

Add a method that returns an object of FilterRegistrationBean, which can create an instance of the Filter object

Create a FilterRegistrationBean object and pass in the instantiated Filter object

Add Url,bean.addUrlPatterns ()

Returns the FilterRegistrationBean object

Add @ Bean to this method

/ * Register Filter project www.1b23.com * / @ Beanpublic FilterRegistrationBean getFilterRegistrationBean () {FilterRegistrationBean bean = new FilterRegistrationBean (new SecondFilter ()); / / bean.addUrlPatterns (new String [] {"* .do", "* .jsp"}); bean.addUrlPatterns ("/ second"); return bean } this is the end of how to configure Filter filtering and blocking in SpringBoot. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Object method annotation project configuration content instance interface article more knowledge article component good code multiple array way this method type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL Huawei Shulou Technology OPPO Reno