您好,登錄后才能下訂單哦!
小編給大家分享一下SpringBoot注冊Filter的方法有哪些,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
springboot版本:2.2.5
一、filter注冊
springboot中添加filter有兩種方式:
1、實現方法一
package com.example.demo.filter1; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.springframework.stereotype.Component; /** * Springboot實現filter的方式一: * 實現Filter接口,并使用@Component注解 * 需要注意的是:這個方式注冊的filter的執行順序優先級較低 * @author Administrator * */ @Component public class FilterSample1 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("這里是"+this.getClass().getName()+"的init"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("這里是"+this.getClass().getName()+"的doFilter"); chain.doFilter(request,response); } @Override public void destroy() { System.out.println("這里是"+this.getClass().getName()+"的doFilter"); } }
2、實現方法二
package com.example.demo.filter2; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class FilterSample2 implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("這里是"+this.getClass().getName()+"的doFilter"); chain.doFilter(request,response); } }
package com.example.demo.filter2; import java.util.ArrayList; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Springboot注冊filter方法二: * 同樣要實現filter接口,但filter注冊使用@Bean的方式進行注冊,可以設置filter順序 * @author Administrator * */ @Configuration public class WebConfig { @Bean public FilterRegistrationBean<FilterSample2> filterSample2() { FilterRegistrationBean<FilterSample2> registrationBean = new FilterRegistrationBean<FilterSample2>(); FilterSample2 myFilter = new FilterSample2(); registrationBean.setFilter(myFilter); ArrayList<String> urls = new ArrayList<>(); urls.add("/*");//配置過濾規則 registrationBean.setUrlPatterns(urls); registrationBean.setOrder(3); return registrationBean; } }
二、Springboot自帶filter
Springboot默認啟用了三個filter,他們是requestContextFilter,characterEncodingFilter,formContentFilter,可以通過在application.yml中設置debug: true來驗證如下:
2020-03-17 22:13:17.254 DEBUG 2700 --- [ main] o.s.b.w.s.f.OrderedRequestContextFilter : Filter 'requestContextFilter' configured for use
2020-03-17 22:13:17.255 DEBUG 2700 --- [ main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2020-03-17 22:13:17.255 DEBUG 2700 --- [ main] o.s.b.w.s.f.OrderedFormContentFilter : Filter 'formContentFilter' configured for use
三、查看應用中注冊了哪些filter
#application.yml debug: true
如上圖,啟用debug模式,查看日志可得:
標藍部分詳細內容如下:
2020-03-17 22:13:17.244 DEBUG 2700 --- [ main] o.s.b.w.s.ServletContextInitializerBeans :
Mapping filters:
filterRegistrationBean urls=[/*] order=3,
characterEncodingFilter urls=[/*] order=-2147483648,
formContentFilter urls=[/*] order=-9900,
requestContextFilter urls=[/*] order=-105,
filterSample1 urls=[/*] order=2147483647
可以看出,該應用注冊了五個filter,執行順序由order決定
看完了這篇文章,相信你對SpringBoot注冊Filter的方法有哪些有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。