您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在springboot中實現一個@WebFilter注解過濾器,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
@WebFilter注解過濾器
@WebFilter加在過濾器的注解上使用
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 ""; /**配置參數*/ WebInitParam[] initParams() default {}; /**Filter名稱*/ String filterName() default ""; String smallIcon() default ""; String largeIcon() default ""; /**指定對哪些Servlet進行過濾*/ String[] servletNames() default {}; /**指定攔截的路徑*/ String[] value() default {}; /**指定攔截的路徑*/ String[] urlPatterns() default {}; /**指定Filter對哪種方式的請求進行過濾*/ DispatcherType[] dispatcherTypes() default {DispatcherType.REQUEST}; /**指定Filter是否支持異步模式*/ boolean asyncSupported() default false; }
在springBoot的啟動類中加入注解:
import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan
多個@WebFilter注解的過濾器可以配合@Order()注解實現執行過濾的順序
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"); } }
執行結果
WebFilterTest --- doFilter
2 --- WebFilterTest2 --- doFilter
不使用注解的方式使用過濾器
創建過濾器類
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"); } }
注冊過濾器
@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; } }
多個過濾器注冊
再添加一個過濾器:
@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; } }
關于怎么在springboot中實現一個@WebFilter注解過濾器就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。