SpringBoot学习笔记:过滤器监听器拦截器
Posted 听风者-better
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot学习笔记:过滤器监听器拦截器相关的知识,希望对你有一定的参考价值。
本文介绍springboot中使用过滤器、监听器、拦截器
过滤器
过滤器,就是用来过滤web请求的
1)过滤器可以过滤所有的web 请求,包括静态页面
2)过滤器依赖于Servlet,所以只能过滤web请求,一般用来过滤敏感词、URL级别的访问权限控制等
监听器
监听器实际上是一个特殊的servlet。他的作用就是监听servletContext、HttpSession、ServeltRequest等对象的的创建或者销毁以及属性值发生改变的事件
一般用于统计在线人数、访问量等
拦截器
1)拦截器只能拦截action请求,不拦截静态页面等
2)拦截器基于反射机制实现,依赖于Spring
具体代码实现:
一.过滤器
1.使用配置类的方式
//定义过滤器
@Slf4j
public class MyFilter implements Filter
@Override
public void init(FilterConfig filterConfig) throws ServletException
log.info("MyFilter过滤器初始化");
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
log.info("MyFilter过滤器开始执行");
filterChain.doFilter(servletRequest, servletResponse);
log.info("MyFilter过滤器结束执行");
@Override
public void destroy()
log.info("MyFilter过滤器销毁");
//过滤器配置
@Configuration
public class FilterConfig
@Bean
public FilterRegistrationBean registrationBean()
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new MyFilter());
bean.addUrlPatterns("/*");
bean.setName("myFilter");
bean.setOrder(1);//设置过滤的优先级
return bean;
2.使用注解的方式
//定义过滤器
@Slf4j
@WebFilter(urlPatterns = "/test/*",filterName = "myFilter")
public class MyFilter implements Filter
@Override
public void init(FilterConfig filterConfig) throws ServletException
log.info("MyFilter过滤器初始化");
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
log.info("MyFilter过滤器开始执行");
filterChain.doFilter(servletRequest, servletResponse);
log.info("MyFilter过滤器结束执行");
@Override
public void destroy()
log.info("MyFilter过滤器销毁");
//启动类
@SpringBootApplication
@ServletComponentScan("com.twy.springbootfilter.filter")
public class SpringbootFilterApplication
public static void main(String[] args)
SpringApplication.run(SpringbootFilterApplication.class, args);
二、监听器
实现一个简单的用户在线人数demo
1.使用配置类的方式
//定义监听器
@Slf4j
public class MyListener implements HttpSessionListener
private int count = 0;
@Override
public void sessionCreated(HttpSessionEvent se)
count++;
log.info("用户上线了");
se.getSession().getServletContext().setAttribute("count", count);
@Override
public void sessionDestroyed(HttpSessionEvent se)
count--;
log.info("用户下线了");
se.getSession().getServletContext().setAttribute("count", count);
//配置类
@Configuration
public class ListenerConfig
@Bean
public ServletListenerRegistrationBean ServletListenerRegistrationBean()
ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<>();
registrationBean.setListener(new MyListener());
return registrationBean;
//控制层
@RestController
public class HelloController
@GetMapping("/count")
public String count(HttpServletRequest request)
return "在线人数:" + request.getSession().getServletContext().getAttribute("count");
@RequestMapping("/logOut")
public String logOut(HttpServletRequest request)
request.getSession().invalidate();
return "注销成功";
打开两个浏览器分别请求127.0.0.1:8080/count
,请求创建了session,执行了监听器里sessionCreated方法,结果如下
请求127.0.0.1:8080/logOut
注销了session,执行了监听器里sessionDestroyed方法,结果如下
2.使用注解的方式
和过滤器一样,添加@WebListener
,@ServletComponentScan
即可
三、拦截器
//定义拦截器
@Slf4j
public class MyInterceptor 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 InterceptorConfig implements WebMvcConfigurer
@Override
public void addInterceptors(InterceptorRegistry registry)
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
需要注意的是preHandle()
方法只有返回true
,方法才会执行
以上是关于SpringBoot学习笔记:过滤器监听器拦截器的主要内容,如果未能解决你的问题,请参考以下文章