springboot @WebFilter注解過(guò)濾器的實(shí)現(xiàn)
@WebFilter注解過(guò)濾器
@WebFilter加在過(guò)濾器的注解上使用
import lombok.extern.slf4j.Slf4j; 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 javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(urlPatterns = "/*") @Slf4j public class WebFilterTest implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("WebFilterTest --- init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("WebFilterTest --- doFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { log.info("WebFilterTest --- destroy"); } }
@WebFilter源碼:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface WebFilter { String description() default ""; /**Filter顯示名*/ String displayName() default ""; /**配置參數(shù)*/ WebInitParam[] initParams() default {}; /**Filter名稱*/ String filterName() default ""; String smallIcon() default ""; String largeIcon() default ""; /**指定對(duì)哪些Servlet進(jìn)行過(guò)濾*/ String[] servletNames() default {}; /**指定攔截的路徑*/ String[] value() default {}; /**指定攔截的路徑*/ String[] urlPatterns() default {}; /**指定Filter對(duì)哪種方式的請(qǐng)求進(jìn)行過(guò)濾*/ DispatcherType[] dispatcherTypes() default {DispatcherType.REQUEST}; /**指定Filter是否支持異步模式*/ boolean asyncSupported() default false; }
在springBoot的啟動(dòng)類中加入注解:
import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan
多個(gè)@WebFilter注解的過(guò)濾器可以配合@Order()注解實(shí)現(xiàn)執(zhí)行過(guò)濾的順序
import org.springframework.core.annotation.Order; @WebFilter(urlPatterns = "/*") @Slf4j @Order(1) public class WebFilterTest implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("WebFilterTest --- init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("WebFilterTest --- doFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { log.info("WebFilterTest --- destroy"); } }
@WebFilter(urlPatterns = "/*") @Slf4j @Order(2) public class WebFilterTest2 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("2---WebFilterTest2 --- init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("2 --- WebFilterTest2 --- doFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { log.info("WebFilterTest2 --- destroy"); } }
執(zhí)行結(jié)果
WebFilterTest --- doFilter
2 --- WebFilterTest2 --- doFilter
不使用注解的方式使用過(guò)濾器
創(chuàng)建過(guò)濾器類
public class WebFilterTest implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("WebFilterTest --- init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("WebFilterTest --- doFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { log.info("WebFilterTest --- destroy"); } }
注冊(cè)過(guò)濾器
@Configuration public class FilterConfig { @Bean public WebFilterTest webFilterTest(){ return new WebFilterTest(); } @Bean public FilterRegistrationBean filterRegist(){ FilterRegistrationBean frBean = new FilterRegistrationBean(); frBean.setFilter(webFilterTest()); frBean.setOrder(1); frBean.addUrlPatterns("/*"); return frBean; } }
多個(gè)過(guò)濾器注冊(cè)
再添加一個(gè)過(guò)濾器:
@Slf4j public class WebFilterTest2 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("2---WebFilterTest2 --- init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("2 --- WebFilterTest2 --- doFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { log.info("WebFilterTest2 --- destroy"); } }
修改配置類:
@Configuration public class FilterConfig { @Bean public WebFilterTest webFilterTest(){ return new WebFilterTest(); } @Bean public WebFilterTest2 webFilterTest2(){ return new WebFilterTest2(); } @Bean public FilterRegistrationBean filterRegist(){ FilterRegistrationBean frBean = new FilterRegistrationBean(); frBean.setFilter(webFilterTest()); frBean.setOrder(1); frBean.addUrlPatterns("/*"); return frBean; } @Bean public FilterRegistrationBean filterRegist2(){ FilterRegistrationBean frBean = new FilterRegistrationBean(); frBean.setFilter(webFilterTest2()); frBean.setOrder(2); frBean.addUrlPatterns("/*"); return frBean; } }
到此這篇關(guān)于springboot @WebFilter注解過(guò)濾器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot @WebFilter注解過(guò)濾器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合cxf發(fā)布webservice以及調(diào)用的方法
這篇文章主要介紹了springboot整合cxf發(fā)布webservice以及調(diào)用的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08使用多種方式實(shí)現(xiàn)遍歷HashMap的方法
下面小編就為大家?guī)?lái)一篇使用多種方式實(shí)現(xiàn)遍歷HashMap的方法。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解
這篇文章主要為大家介紹了MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07SpringBoot中Redisson延遲隊(duì)列的示例
延時(shí)隊(duì)列是一種常見的需求,延時(shí)隊(duì)列允許我們延遲處理某些任務(wù),本文主要介紹了Redisson延遲隊(duì)列的示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06SpringBoot之HandlerInterceptor攔截器的使用詳解
這篇文章主要介紹了SpringBoot之HandlerInterceptor攔截器的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10