过滤器 拦截器 监听器
过滤器
实现
实现Filter
类,然后重写它的3个方法:
init
方法 : 在容器中创建当前过滤器的时候自动调用这个方法destory
方法 : 在容器中销毁当前过滤器的时候自动调用这个方法doFilter
方法 : 这个方法有3个参数,分别是ServletRequest
,ServletResponse
和FilterChain
.可以在参数中获取HttpServletRequest
和HttpServletResponse
对象进行相应的处理操作.
在springboot项目中使用filter ,自定义MyFilter
类:
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.http.HttpServletRequest;
import java.io.IOException;
@Slf4j
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("init 方法被调用");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
log.info("doFilter 方法被调用");
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
String s = httpServletRequest.getRequestURL().toString();
log.info("url : "+s);
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
log.info("destroy 方法被调用");
}
}
然后在添加到filterChain
中
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {
@Bean
public FilterRegistrationBean filterRegist(){
FilterRegistrationBean frBean = new FilterRegistrationBean();
frBean.setFilter(new MyFilter());
frBean.addUrlPatterns("/*");
return frBean;
}
}
init
方法在项目启动的时候会调用
doFilter
方法在每次请求的时候会调用
过滤器也可以配合@WebFilter
和@ServletComponentScan
使用,详情参见@WebFilter
拦截器
java中拦截器是动态拦截action调用的对象,然后提供可以在action执行前后增加一些操作,也可以在action执行前停止操作.拦截器可以做和过滤器同样的操作
springBoot 中使用拦截器
@Slf4j
@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info(" preHandle 方法");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info(" postHandle 方法");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info(" afterCompletion 方法");
}
}
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {
@Bean
public LoginHandlerInterceptor loginHandlerInterceptor(){ return new LoginHandlerInterceptor();}
@Override
public void addInterceptors(InterceptorRegistry registry) {
List<String> excludes = new ArrayList<>();
excludes.add("/api/**");
excludes.add("/js/**");
excludes.add("/css/**");
excludes.add("/img/**");
excludes.add("/lib/**");
excludes.add("/fonts/**");
registry.addInterceptor(loginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns(excludes);
}
}
监听器
监听器通常用于监听Web应用中对象的创建,销毁等动作的发生,同时对监听的情况作出相应的处理,最常用于统计网站的在线人数,访问量等信息
- ServletContextListener : 用来监听ServletContext属性的操作,比如新增修改删除
- HttpSessionListener : 用来监听Web应用中的Session对象,常用于统计在线情况
- ServletRequestListener : 用来监听Request对象的属性操作
使用监听器需要在类中实现对应功能的监听器对象,例如实现 HttpSessionListener 类
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@Slf4j
public class MyHttpSessionListener implements HttpSessionListener {
private static int online = 0;
@Override
public void sessionCreated(HttpSessionEvent event){
log.info("sessionCreated 方法");
online++;
log.info("online : "+online);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
log.info("sessionDestroyed 方法");
online--;
log.info("online : "+online);
}
}
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {
@Bean
public ServletListenerRegistrationBean listenerRegistrationBean(){
ServletListenerRegistrationBean srb = new ServletListenerRegistrationBean();
srb.setListener(new MyHttpSessionListener());
return srb;
}
}
监听器可以在MyHttpSessionListener
类上添加注解@WebListener
,并且在启动类上加上@ServletComponentScan
这样就不用在WebAppConfigurer
配置类中配置了
import javax.servlet.annotation.WebListener;
@WebListener
@Slf4j
public class MyHttpSessionListener implements HttpSessionListener {
private static int online = 0;
@Override
public void sessionCreated(HttpSessionEvent event){
log.info("sessionCreated 方法");
online++;
log.info("online : "+online);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
log.info("sessionDestroyed 方法");
online--;
log.info("online : "+online);
}
}
本篇文章由一文多发平台ArtiPub自动发布