zookeeper python接口实例详解
415
2022-12-12
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.anhttp://notation.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(FilterConfihttp://g 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;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~