以下是网友观点:
springboot配置监听器、过滤器和拦截器
监听器:listener是servlet规范中定义的一种特殊类。用于监听servletContext、HttpSession和servletRequest等域对象的创建和销毁事件。监听域对象的属性发生修改的事件。用于在事件发生前、发生后做一些必要的处理。其主要可用于以下方面:1、统计在线人数和在线用户2、系统启动时加载初始化信息3、统计网站访问量4、记录用户访问路径。
过滤器:Filter是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。它主要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理。使用Filter的完整流程:Filter对用户请求进行预处理,接着将请求交给Servlet进行处理并生成响应,最后Filter再对服务器响应进行后处理。
拦截器:Interceptor 在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。比如日志,安全等。一般拦截器方法都是通过动态代理的方式实现。可以通过它来进行权限验证,或者判断用户是否登陆,或者是像12306 判断当前时间是否是购票时间。
三大器在springboot中使用时,首先实现相应的接口定义类,然后通过配置类将其加入到spring容器中,从而实现相应的功能。代码如下:
1、过滤器类
package com.example.demo; import java.io.IOException; 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 javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; public class MyFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println(servletRequest.getParameter("name")); HttpServletRequest hrequest = (HttpServletRequest)servletRequest; HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse); if(hrequest.getRequestURI().indexOf("/index") != -1 || hrequest.getRequestURI().indexOf("/asd") != -1 || hrequest.getRequestURI().indexOf("/online") != -1 || hrequest.getRequestURI().indexOf("/login") != -1 ) { filterChain.doFilter(servletRequest, servletResponse); }else { wrapper.sendRedirect("/login"); } } @Override public void destroy() { } @Override public void init(FilterConfig filterConfig) throws ServletException { } }
2、监听器类
package com.example.demo; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class MyHttpSessionListener implements HttpSessionListener { public static int online = 0; @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("创建session"); online ++; } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("销毁session"); } }
3、拦截器类
package com.example.demo; import java.io.PrintWriter; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ModelAndView; public class MyInterceptor implements HandlerInterceptor { //在请求处理之前进行调用(Controller方法调用之前 @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("preHandle被调用"); Map map =(Map)httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); System.out.println(map.get("name")); System.out.println(httpServletRequest.getParameter("username")); if(map.get("name").equals("zhangsan")) { return true; //如果false,停止流程,api被拦截 }else { PrintWriter printWriter = httpServletResponse.getWriter(); printWriter.write("please login again!"); return false; } } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle被调用"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion被调用"); } }
4、配置类
package com.example.demo; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MywebConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/zxc/foo").setViewName("foo"); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()) .addPathPatterns("/asd/**"); } @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public FilterRegistrationBean filterRegist() { FilterRegistrationBean frBean = new FilterRegistrationBean(); frBean.setFilter(new MyFilter()); frBean.addUrlPatterns("/*"); System.out.println("filter"); return frBean; } @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public ServletListenerRegistrationBean listenerRegist() { ServletListenerRegistrationBean srb = new ServletListenerRegistrationBean(); srb.setListener(new MyHttpSessionListener()); System.out.println("listener"); return srb; } }
5、控制层
package com.example.demo; import java.util.Date; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { private final Logger logger = LoggerFactory.getLogger(UserController.class); @Value("${application.message:Hello World}") private String message ; @GetMapping("/asd/{name}") public String welcome(@PathVariable String name,Map<String, Object> model) { model.put("time", new Date()); model.put("message", this.message); return "welcome"; } @RequestMapping("/login") @ResponseBody public Object foo() { logger.info("打印日志----------------------"); return "login"; } @RequestMapping("/index") @ResponseBody public Object index(HttpServletRequest request) { HttpSession session = request.getSession(true); session.setAttribute("zxc", "zxc"); return "index"; } @RequestMapping("/online") @ResponseBody public Object online() { return "当前在线人数:" + MyHttpSessionListener.online + "人"; } }
6、程序主入口及application.properties文件默认。
项目启动后,首先测试过滤器,所有访问路径都需经过我们自定义的filter,如果路径中包含/index、/asd、/online、/login则直接通过,否则则被重定向至/login。
然后测试监听器,先访问http://localhost:8080/index,创建session对象(因为 session对象创建需要显示的调用getsession方法),然后访问http://localhost:8080/online,然后换一个浏览器做相同操作,如下:
最后测试拦截器,代码展示了如何获取@PathVariable注解的请求参数以及普通请求参数,首先访问http://localhost:8080/asd/zhangsan?username=lisi,响应正常,如果访问路径上的名字不是zhangsan,则被拦截,例如http://localhost:8080/asd/wangwu?username=lisi,如下:
在实际项目中我们还可以在配置的时候设置过滤器、拦截器的执行顺序及其它的参数,同时filter和listener还有对应的注解方式:@WebFilter和@WebListener,在使用注解方式时不要忘了在主程序加上@ServletComponentScan注解,这样才能在程序启动时将对应的bean加载进来。