springboot(五)filter/interceptor/listener

来源:互联网 发布:北京柒柒网络团队骗 编辑:程序博客网 时间:2024/06/05 04:35

当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。
Spring boot 的主 Servlet 为DispatcherServlet,其默认的url-pattern为“/”。
过滤器属于Servlet范畴的API,与spring 没什么关系。
Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器)。

HandlerInterceptor 的功能跟过滤器类似,但是提供更精细的的控制能力:在request被响应之前、request被响应之后、视图渲染之前以及request全部结束之后。我们不能通过拦截器修改request内容,但是可以通过抛出异常(或者返回false)来暂停request的执行。

springboot 为过滤器和监听器提供了两种实现方式。
1.application注册
2.注解
自定义注解过滤器如:

@WebFilter(filterName = "authorizeFilter", urlPatterns = "/*")public class BasicAuthorizeFilter implements Filter {private final static Logger LOGGER = LoggerFactory.getLogger(BasicAuthorizeFilter.class);@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {    // TODO Auto-generated method stub    LOGGER.info("<<<<<<<<<<<<<<<<<<<<<过滤器处理中>>>>>>>>>>>>>>>>>>>>>>>>");    chain.doFilter(request, response);}@Overridepublic void init(FilterConfig arg0) throws ServletException {    // TODO Auto-generated method stub    LOGGER.info("\"<<<<<<<<<<<<<<<<<<<<<过滤器开始了>>>>>>>>>>>>>>>>>>>>>>>>\"");}@Overridepublic void destroy() {    // TODO Auto-generated method stub    LOGGER.info("<<<<<<<<<<<<<<<<<<<<<过滤器结束了>>>>>>>>>>>>>>>>>>>>>>>>");    }}

并且在Application中,加上注解@ServletComponentScan实现Servlet扫描。

第二种在Application注册,添加相应的过滤器

@Beanpublic FilterRegistrationBean filterRegistrationBean() {    FilterRegistrationBean registrationBean = new FilterRegistrationBean();    BasicAuthorizeFilter httpBasicFilter = new BasicAuthorizeFilter();    registrationBean.setFilter(httpBasicFilter);    List<String> urlPatterns = new ArrayList<>();    urlPatterns.add("/*");    registrationBean.setUrlPatterns(urlPatterns);    return registrationBean;}

自定义拦截器:

public class MyInterceptor implements HandlerInterceptor {private final static Logger LOGGER = LoggerFactory.getLogger(MyInterceptor.class);@Overridepublic boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {    LOGGER.info(">>>MyInterceptor>>>>>>>在请求处理之前进行调用");    return true;// 只有返回true才会继续向下执行,返回false取消当前请求}@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)        throws Exception {    LOGGER.info(">>>MyInterceptor>>>>>>>请求处理之后进行调用,但是在视图被渲染之前");}@Overridepublic void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)        throws Exception {    LOGGER.info(">>>MyInterceptor>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行");    }}

自定义监听器:

@WebListenerpublic class MyServletContextListener implements ServletContextListener {private final  static Logger LOGGER = LoggerFactory.getLogger(MyServletContextListener.class);@Overridepublic void contextInitialized(ServletContextEvent sce) {    LOGGER.info("<<<<<<<<<<<<<<<<<<上下文监听器开始了>>>>>>>>>>>>>>>>>>>");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {    System.out.println("<<<<<<<<<<<<<<<<<<上下文监听器结束了>>>>>>>>>>>>>>>>>>>");    }  }