怎么在springboot中实现一个@WebFilter注解过滤器
这篇文章给大家介绍怎么在springboot中实现一个@WebFilter注解过滤器,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
沅陵ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
@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注解过滤器
链接地址:http://scyanting.com/article/jcjios.html