Springboot實(shí)現(xiàn)過濾器的兩種方式
Springboot實(shí)現(xiàn)過濾器有以下倆種方式,第一種方式倆步走,即向spring容器注冊filter
第一種:
1.Filter過濾器具體實(shí)現(xiàn)類
@Component
@Slf4j
public class MyTestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("[ {} ] 創(chuàng)建啦...", this.getClass().getSimpleName());
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("[ {} ] 執(zhí)行啦...", this.getClass().getSimpleName());
chain.doFilter(request, response);
}
@Override
public void destroy() {
log.info("[ {} ] 被摧毀啦...", this.getClass().getSimpleName());
}
}2.向spring容器注冊filter
@Configuration
public class FilterConfig {
@Resource
private MyTestFilter myTestFilter;
@Bean
public FilterRegistrationBean testFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(myTestFilter);
registration.addUrlPatterns("/filter/*");
registration.setName("testFilter");
registration.setOrder(1);
return registration;
}
}第二種:
通過@WebFilter 注解來配置
@Component
@WebFilter(urlPatterns = "/filter/*", filterName = "myTestFilter")
@Slf4
public class MyTestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("[ {} ] 創(chuàng)建啦...", this.getClass().getSimpleName());
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("[ {} ] 執(zhí)行啦...", this.getClass().getSimpleName());
chain.doFilter(request, response);
}
@Override
public void destroy() {
log.info("[ {} ] 被摧毀啦...", this.getClass().getSimpleName());
}
}還需要在 SpringBootApplication 上使用@ServletComponentScan
注解后
Servlet可以直接通過@WebServlet注解自動注冊
Filter可以直接通過@WebFilter注解自動注冊
Listener可以直接通過@WebListener 注解自動注冊
到此這篇關(guān)于Springboot實(shí)現(xiàn)過濾器的文章就介紹到這了,更多相關(guān)Springboot過濾器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一口氣說出Java 6種延時隊(duì)列的實(shí)現(xiàn)方法(面試官也得服)
這篇文章主要介紹了一口氣說出Java 6種延時隊(duì)列的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
springboot+dynamicDataSource動態(tài)添加切換數(shù)據(jù)源方式
這篇文章主要介紹了springboot+dynamicDataSource動態(tài)添加切換數(shù)據(jù)源方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
IDEA?2022最新激活碼注冊碼超詳細(xì)教程(親測激活有效)
這篇文章主要介紹了IDEA?2022最新激活碼超詳細(xì)教程(親測激活至2099年),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Java圖形化界面設(shè)計(jì)之布局管理器之BorderLayout案例詳解
這篇文章主要介紹了Java圖形化界面設(shè)計(jì)之布局管理器之BorderLayout案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
SpringBoot整合JWT(JSON?Web?Token)生成token與驗(yàn)證的流程及示例
JSON Web Token(JWT)是一種開放的標(biāo)準(zhǔn)(RFC 7519),定義了一種緊湊的、自包含的方式來安全地在各方之間傳輸信息作為JSON對象,這篇文章主要給大家介紹了關(guān)于SpringBoot整合JWT(JSON?Web?Token)生成token與驗(yàn)證的相關(guān)資料,需要的朋友可以參考下2024-07-07
java根據(jù)模板實(shí)現(xiàn)填充word內(nèi)容并轉(zhuǎn)換為pdf
這篇文章主要為大家詳細(xì)介紹了java如何根據(jù)模板實(shí)現(xiàn)填充word內(nèi)容并轉(zhuǎn)換為pdf,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04

