时间:2021-05-19
@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 = "/*")@Slf4jpublic 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)@Documentedpublic @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"); }}注册过滤器
@Configurationpublic 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; }}多个过滤器注册
再添加一个过滤器:
@Slf4jpublic 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"); }}修改配置类:
@Configurationpublic 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注解过滤器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
有2种方式可以实现过滤器1:通过FilterRegistrationBean实例注册2:通过@WebFilter注解生效这里选择第一种,因为第二种不能设置过滤器
SpringBoot中实现一个过滤器相当简单,实现javax.servlet.Filter接口即可。下面以实现一个记录接口访问日志及请求耗时的过滤器为例:1、定
过滤器(Filter)的注册方法和Servlet一样,有两种方式:代码注册或者注解注册1.代码注册方式通过代码方式注入过滤器@BeanpublicFilterR
AngularJS过滤器过滤器可以使用一个管道字符(|)添加到表达式和指令中。AngularJS过滤器AngularJS过滤器可用于转换数据:过滤器描述curr
vue2.0里,不再有自带的过滤器,需要自己定义过滤器。定义的方法如下:注册一个自定义过滤器,它接收两个参数:过滤器ID和过滤器函数。Vue.filter('f