[Interceptor] SpringMVC

I. Overview

1.1 interceptor abnormal situation

  • User requests to DispatherServlet in, DispatherServlet call HandlerMapping find Handler, HandlerMapping interception return of a chain of children (more than interception), springmvc the interceptor was launched by HandlerMapping.
  • In the enterprise development, the use of interceptors user authentication (user identity verification intercepted after landing), user rights interception.

1.2 interceptor Method

public class HandlerInterceptor1 implements HandlerInterceptor {

    //在执行handler之前来执行的
    //用于用户认证校验、用户权限校验
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        
        System.out.println("HandlerInterceptor1...preHandle");
        
        //如果返回false表示拦截不继续执行handler,如果返回true表示放行
        return false;
    }
    //在执行handler返回modelAndView之前来执行
    //如果需要向页面提供一些公用 的数据或配置一些视图信息,使用此方法实现 从modelAndView入手
    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("HandlerInterceptor1...postHandle");
        
    }
    //执行handler之后执行此方法
    //作系统 统一异常处理,进行方法执行性能监控,在preHandle中设置一个时间点,在afterCompletion设置一个时间,两个时间点的差就是执行时长
    //实现 系统 统一日志记录
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("HandlerInterceptor1...afterCompletion");
    }

}

Second, the example

2.1 Definitions two interceptors

2.2 Configuration interceptor

  • Configure the global interceptors, DispatcherServlet configure global interceptors loaded into all HandlerMapping.
  • In springmvc.xml configure:
    <!--拦截器 -->
    <mvc:interceptors>
        <!--多个拦截器,顺序执行 -->
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.hao.ssm.controller.interceptor.HandlerInterceptor1"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.hao.ssm.controller.interceptor.HandlerInterceptor2"></bean>
        </mvc:interceptor> 
        
        
    </mvc:interceptors>

2.3 the order of execution

  • PreHandle execution order is executed.
  • Execution postHandle, afterCompletion reverse order execution
  • If preHandle not released, postHandle, afterCompletion not execute.
  • As long as there is not an interceptor release, controller can not perform complete
  • Only the front of the interceptor method preHandle release, interceptor preHandle below have to be performed.

Third, the interceptor application

3.1 Requirements

  • Resource users to access the system (url), if the user does not authenticate, intercept, system jumps landing page, if the user has been authenticated, the user can continue to access the system resources.

3.2 user login and exit the function development

@Controller
public class LoginController {
    
    
    //用户登陆提交方法
    @RequestMapping("/login")
    public String login(HttpSession session, String usercode,String password)throws Exception{
        
        //调用service校验用户账号和密码的正确性
        //..
        
        //如果service校验通过,将用户身份记录到session
        session.setAttribute("usercode", usercode);
        //重定向到商品查询页面
        return "redirect:/items/queryItems.action";
    }
    
    //用户退出
    @RequestMapping("/logout")
    public String logout(HttpSession session)throws Exception{
        
        //session失效
        session.invalidate();
        //重定向到商品查询页面
        return "redirect:/items/queryItems.action";
        
    }
    

}

3.3 user authentication check interceptors

  • Interception realization of ideas:

3.4 write blocker

public class LoginInterceptor implements HandlerInterceptor {

    //在执行handler之前来执行的
    //用于用户认证校验、用户权限校验
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        
        //得到请求的url
        String url = request.getRequestURI();
        
        //判断是否是公开 地址
        //实际开发中需要公开 地址配置在配置文件中
        //...
        if(url.indexOf("login.action")>=0){
            //如果是公开 地址则放行
            return true;
        }
        
        //判断用户身份在session中是否存在
        HttpSession session = request.getSession();
        String usercode = (String) session.getAttribute("usercode");
        //如果用户身份在session中存在放行
        if(usercode!=null){
            return true;
        }
        //执行到这里拦截,跳转到登陆页面,用户进行身份认证
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        
        //如果返回false表示拦截不继续执行handler,如果返回true表示放行
        return false;
    }
    //在执行handler返回modelAndView之前来执行
    //如果需要向页面提供一些公用 的数据或配置一些视图信息,使用此方法实现 从modelAndView入手
    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("HandlerInterceptor1...postHandle");
        
    }
    //执行handler之后执行此方法
    //作系统 统一异常处理,进行方法执行性能监控,在preHandle中设置一个时间点,在afterCompletion设置一个时间,两个时间点的差就是执行时长
    //实现 系统 统一日志记录
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("HandlerInterceptor1...afterCompletion");
    }

}

  • Configuring interceptors in the configuration file
<!--拦截器 -->
    <mvc:interceptors>
        <!--多个拦截器,顺序执行 -->
        <!-- <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.hao.ssm.controller.interceptor.HandlerInterceptor1"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.hao.ssm.controller.interceptor.HandlerInterceptor2"></bean>
        </mvc:interceptor> -->
        
        <mvc:interceptor>
            <!-- /**可以拦截路径不管多少层 -->
            <mvc:mapping path="/**" />
            <bean class="com.hao.ssm.controller.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

Guess you like

Origin www.cnblogs.com/haoworld/p/springmvc-lan-jie-qi.html