springboot自定義過濾器的方法
過濾器是Servlet的規(guī)范,是基于函數(shù)回調(diào)的,需要實現(xiàn)javax.servlet.Filter接口,依賴于Tomcat等容器,一般用于過濾請求的URL。
1自定義過濾器
自定義filter的實現(xiàn),本質(zhì)上只有一種方式,就是實現(xiàn)Filter接口。但是在spring中我們有時候也會通過繼承框架提供的XXXFilter,例如OncePerRequestFilter、 AbstractAuthenticationProcessingFilter(Spring Security使用的認證過濾器),當然,這些過濾器所實現(xiàn)的頂層接口還是Filter,只不過框架提供的過濾器都是有其特殊職能的,我們自己實現(xiàn)過濾器基本通過下面兩種方法。
1.1實現(xiàn)Filter接口
public class MyFilterOne implements Filter {
?
? ? @Override
? ? public void destroy() {
? ? ? ? //服務(wù)停止時銷毀;
? ? }
?
? ? @Override
? ? public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
? ? ? ? ? ? throws IOException, ServletException {
? ? ? ??
? ? ? ? System.out.println("this is MyFilter,url :" + request.getRequestURI());
? ? ? ? //1、進入過濾器,通常在這里根據(jù)requet做一些事情
? ? ? ? HttpServletRequest request = (HttpServletRequest) srequest;
? ? ? ? //2、放行,進入下一個過濾器,如果是最后一個過濾器,就執(zhí)行Controller代碼
? ? ? ? filterChain.doFilter(srequest, sresponse);
? ? ? ? //3、回到過濾器,通常在這里對response做一些處理
? ? ? ? HttpServletResponse response = (HttpServletResponse) srequest;
? ? }
?
? ? @Override
? ? public void init(FilterConfig arg0) throws ServletException {
? ? ? ? //服務(wù)啟動時創(chuàng)建;
? ? }
?
}1.2繼承OncePerRequestFilter
下面的實現(xiàn),并沒有配置過濾路徑,所有的請求都會進入到這個過濾器,但是它通過@Value獲取配置的url列表,然后用這個列表去和進入過濾器的請求進行對比,如果匹配就做一些操作,如果不匹配直接放行。個人覺得還是配置過濾路徑好。
@Component
@Order(-1)
public class MyFilterThree extends OncePerRequestFilter {
?
? ? private final List<Pattern> uriPatterns = new ArrayList<Pattern>();
?
? ? @Value("#{'${filtered.uris:^$}'.split(',')}")
? ? private List<String> filteredUris;
?
? ? @PostConstruct
? ? public void initialize() {
? ? ? ? for (String uri : this.filteredUris) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? this.uriPatterns.add(Pattern.compile(uri));
? ? ? ? ? ? ? ? System.out.println(String.format("符合 '%s' 格式的URI,將進行過濾處理,否則放行.", uri));
?
? ? ? ? ? ? } catch (PatternSyntaxException patternSyntaxException) {
? ? ? ? ? ? ? ? System.out.println("Invalid regular expression pattern in filtered.uris: " + uri);
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? @Override
? ? protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
? ? ? ? System.out.println(httpServletRequest.getRequestURI());
? ? ? ? System.out.println("需要過濾的路徑"+ Arrays.toString(uriPatterns.toArray()));
? ? ? ? System.out.println("進入過濾器了");
? ? ? ? filterChain.doFilter(httpServletRequest, httpServletResponse);//放行
? ? ? ? System.out.println("又回到過濾器了");
? ? }
?
? ? private boolean isMatchedUri(String uri) {
? ? ? ? if (StringUtils.isEmpty(uri)) {
? ? ? ? ? ? return false;
? ? ? ? } else {
? ? ? ? ? ? Iterator var2 = this.uriPatterns.iterator();
?
? ? ? ? ? ? Pattern pattern;
? ? ? ? ? ? do {
? ? ? ? ? ? ? ? if (!var2.hasNext()) {
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? pattern = (Pattern)var2.next();
? ? ? ? ? ? } while(!pattern.matcher(uri).find());
?
? ? ? ? ? ? return true;
? ? ? ? }
? ? }
}1.3使過濾器生效配置
使用配置類
@Configuration
public class MyFilterConfiguration {
?
? ? @Bean
? ? public FilterRegistrationBean registerFilter() {
? ? ? ? System.out.println("MyFilterConfiguration");
? ? ? ? FilterRegistrationBean registration = new FilterRegistrationBean();
? ? ? ? registration.setFilter(new MyFilterOne());
? ? ? ? registration.addUrlPatterns("/public/*");//過濾的路徑
? ? ? ? registration.addInitParameter("paramName", "paramValue");
? ? ? ? registration.setName("MyFilter");
? ? ? ? registration.setOrder(1);//在過濾鏈中的執(zhí)行順序
? ? ? ? return registration;
? ? }
}@WebFilter和@ServletComponentScan(basePackages = "")
個人比較喜歡這個方式,代碼量最小
第一步:在啟動類上添加注解@ServletComponentScan(basePackages = "")
第二步:Filter類添加@WebFilter注解,配置FilterRegistrationBean的屬性@WebFilter基本都有
@WebFilter(urlPatterns = "/selfAnnotation/*")
@Order(-2)
public class MyFilterFive extends OncePerRequestFilter {
?
? ? @Override
? ? protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
?
? ? ? ? System.out.println("進入5號過濾器了");
? ? }
}2 Filter生命周期
init():在構(gòu)造器被調(diào)用后,緊接著被調(diào)用。作用:用來初始化Filter。
doFilter():每一次攔截請求時都會調(diào)用。
destroy():方法在項目停止時調(diào)用,用來在對象被銷毀前做一些收尾工作。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實戰(zhàn)角色權(quán)限后臺腳手架系統(tǒng)的實現(xiàn)流程
只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+myBaits-Plus+Vue+Element-UI+Mysql實現(xiàn)一個角色權(quán)限后臺腳手架系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01
SpringBoot整合Guava Cache實現(xiàn)全局緩存的示例代碼
這篇文章主要介紹了SpringBoot整合Guava Cache實現(xiàn)全局緩存,Guava Cache是Google Guava庫中的一個模塊,提供了基于內(nèi)存的本地緩存實現(xiàn),文中介紹了SpringBoot整合使用Guava Cache的具體步驟,需要的朋友可以參考下2024-03-03
如何在Java中創(chuàng)建線程通信的四種方式你知道嗎
開發(fā)中不免會遇到需要所有子線程執(zhí)行完畢通知主線程處理某些邏輯的場景?;蛘呤蔷€程 A 在執(zhí)行到某個條件通知線程 B 執(zhí)行某個操作。下面我們來一起學習如何解決吧2021-09-09
Spring?boot?運用策略模式實現(xiàn)避免多次使用if
這篇文章主要介紹了Spring?boot?運用策略模式實現(xiàn)避免多次使用if,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09

