springboot @WebFilter注解过滤器的实现
作者:peng
这篇文章主要介绍了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注解过滤器的实现的文章就介绍到这了,更多相关springboot @WebFilter注解过滤器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!