zoukankan      html  css  js  c++  java
  • SpringMvc中Interceptor拦截器用法

        SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆等。

     一. 使用场景

        1、日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。

        2、权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直接返回到登录页面;

        3、性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反      向代理,如apache可以自动记录);

        4、通用行为:读取cookie得到用户信息并将用户对象放入请求,从而方便后续流程使用,还有如提取Locale、Theme信息等,只要是多个处理器都需要的即可使用拦截器实      现。

        5、OpenSessionInView:如Hibernate,在进入处理器打开Session,在完成后关闭Session。

           本质也是AOP(面向切面编程),也就是说符合横切关注点的所有功能都可以放入拦截器实现。

    二. 拦截接口

        

    public interface HandlerInterceptor {  
        boolean preHandle(  
                HttpServletRequest request, HttpServletResponse response,   
                Object handler)   
                throws Exception;  
      
        void postHandle(  
                HttpServletRequest request, HttpServletResponse response,   
                Object handler, ModelAndView modelAndView)   
                throws Exception;  
      
        void afterCompletion(  
                HttpServletRequest request, HttpServletResponse response,   
                Object handler, Exception ex)  
                throws Exception;  
    }   

        preHandle预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器;

           返回值:true表示继续流程(如调用下一个拦截器或处理器);

                   false表示流程中断(如登录检查失败),不会继续调用其他的拦截器或处理器,此时我们需要通过response来产生响应;

        postHandle后处理回调方法,实现处理器的后处理(但在渲染视图之前),此时我们可以通过modelAndView(模型和视图对象)对模型数据进行处理或对视图进行      处理,modelAndView也可能为null。

        afterCompletion整个请求处理完毕回调方法,即在视图渲染完毕时回调,如性能监控中我们可以在此记录结束时间并输出消耗时间,还可以进行一些资源清理,类似      于try-catch-finally中的finally,但仅调用处理器执行链中preHandle返回true的拦截器的afterCompletion

     三. 以下通过一个用户是否登录实例。

      1. 在beans.xml中加入

     <!-- 自定义拦截器 -->
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <bean class="com.haut.phonemanage.interceptor.LoginInterceptor"></bean>
            </mvc:interceptor>
        </mvc:interceptors>

    2、LoginInterceptor

    public class LoginInterceptor implements HandlerInterceptor {
    
        @Override
        public void afterCompletion(HttpServletRequest request,
                HttpServletResponse response, Object handler, Exception exception)
                throws Exception {
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response,
                Object handler, ModelAndView view) throws Exception {
        }
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                Object handler) throws Exception {
            final String ENC = "UTF-8";
            String fullPath = request.getRequestURI();
            String queryString = request.getQueryString();
            String contextPath = request.getContextPath();
            String controllerPath = fullPath.replace(contextPath, "");
            
            HttpSession session = request.getSession();
            Object account = session.getAttribute("account");
            
            if(queryString != null && !queryString.trim().equals("")) {
                queryString = "?" + queryString;
            }
            
            if(!controllerPath.startsWith("/passport") && account == null) {
                response.sendRedirect(contextPath + "/passport?path=" + URLEncoder.encode(contextPath + controllerPath + queryString, ENC));
            }
            
            return true;
        }
        
    }

      总结:很明显拦截器也是aop面向切面编程的一种用法,那么它也就是使用代理模式了。

  • 相关阅读:
    利用DTrace实时检测MySQl
    改进MySQL Order By Rand()的低效率
    RDS for MySQL查询缓存 (Query Cache) 的设置和使用
    RDS For MySQL 字符集相关说明
    RDS for MySQL 通过 mysqlbinlog 查看 binlog 乱码
    RDS for MySQL Mysqldump 常见问题和处理
    RDS for MySQL Online DDL 使用
    RDS MySQL 表上 Metadata lock 的产生和处理
    RDS for MySQL 如何使用 Percona Toolkit
    北京已成为投融资诈骗重灾区:存好骗子公司黑名单,谨防上当!
  • 原文地址:https://www.cnblogs.com/maybo/p/5189540.html
Copyright © 2011-2022 走看看