转载请注明出处:
前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
Spring Web MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。
定义拦截器,实现HandlerInterceptor接口。接口中提供三个方法。
<span style="color: #0000ff">package</span><span style="color: #000000"> joanna.yan.ssm.interceptor; </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletRequest; </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletResponse; </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.servlet.HandlerInterceptor; </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.servlet.ModelAndView; </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> HandlerInterceptor1 <span style="color: #0000ff">implements</span><span style="color: #000000"> HandlerInterceptor{ </span><span style="color: #008000">//</span><span style="color: #008000">执行Handler完成执行此方法 </span><span style="color: #008000">//</span><span style="color: #008000">应用场景:统一异常处理,统一日志处理</span> <span style="color: #000000"> @Override </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception { System.out.println(</span>"HandlerInterceptor1......afterCompletion"<span style="color: #000000">); } </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之后,返回modelAndView之前执行 </span><span style="color: #008000">//</span><span style="color: #008000">应用场景从modelAndView出发:将公用的模型数据(比如菜单导航)在这里传到视图,也可以在这里同意指定视图</span> <span style="color: #000000"> @Override </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception { System.out.println(</span>"HandlerInterceptor1......postHandle"<span style="color: #000000">); } </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之前执行 </span><span style="color: #008000">//</span><span style="color: #008000">用于身份认证、身份授权 </span><span style="color: #008000">//</span><span style="color: #008000">比如身份认证,如果认证不通过表示当前用户没有登录,需要此方法拦截不再向下执行。</span> <span style="color: #000000"> @Override </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception { System.out.println(</span>"HandlerInterceptor1......preHandle"<span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000">return false表示拦截,不向下执行 </span><span style="color: #008000">//</span><span style="color: #008000">return true表示放行</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">; } }</span>
struts中是有一个大的拦截器链,它是一个共用的东西,可以把它添加到任何的action链接,都让它拦截。但是spring的拦截器不是全局的。
springmvc拦截器针对HandlerMapping进行拦截设置,如果在某个HandlerMapping中设置拦截,经过该HandlerMapping映射成功的handler最终使用该拦截器。
<span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="interceptors"</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">list</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">ref </span><span style="color: #ff0000">bean</span><span style="color: #0000ff">="handlerInterceptor1"</span><span style="color: #0000ff">/></span> <span style="color: #0000ff"><</span><span style="color: #800000">ref </span><span style="color: #ff0000">bean</span><span style="color: #0000ff">="handlerInterceptor2"</span><span style="color: #0000ff">/></span> <span style="color: #0000ff"></</span><span style="color: #800000">list</span><span style="color: #0000ff">></span> <span style="color: #0000ff"></</span><span style="color: #800000">property</span><span style="color: #0000ff">></span> <span style="color: #0000ff"></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="handlerInterceptor1"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor1"</span><span style="color: #0000ff">/></span> <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="handlerInterceptor2"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor2"</span><span style="color: #0000ff">/></span>
一般不推荐使用。
springmvc可以配置类似全局的拦截器,springmvc框架将配置的类似全局的拦截器注入到每个HandlerMapping中。
<span style="color: #008000"><!--</span><span style="color: #008000">拦截器 </span><span style="color: #008000">--></span> <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptors</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000">多个拦截器,顺序执行 </span><span style="color: #008000">--></span> <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> /**表示所有url包括子url路径 </span><span style="color: #008000">--></span> <span style="color: #0000ff"><</span><span style="color: #800000">mvc:mapping </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="/**"</span><span style="color: #0000ff">/></span> <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor1"</span><span style="color: #0000ff">></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span> <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span> <span style="color: #0000ff"><</span><span style="color: #800000">mvc:mapping </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="/**"</span><span style="color: #0000ff">/></span> <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor2"</span><span style="color: #0000ff">></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span> <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span> <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptors</span><span style="color: #0000ff">></span>
测试多个拦截器各个方法的执行时机。
运行日志信息:
<span style="color: #000000">HandlerInterceptor1...preHandle HandlerInterceptor2...preHandle HandlerInterceptor2...postHandle HandlerInterceptor1...postHandle HandlerInterceptor2...afterCompletion HandlerInterceptor1...afterCompletion</span>
总结:
preHandle方法按顺序执行,postHandle和afterCompletion按拦截器配置的逆向顺序执行。
运行日志信息:
<span style="color: #000000">HandlerInterceptor1...preHandle HandlerInterceptor2...preHandle HandlerInterceptor1...afterCompletion</span>
总结:
拦截器1放行,拦截器2的preHandle才会执行。
拦截器2的preHandle不放行,拦截器2的postHandle和afterCompletion不会执行。
只要有一个拦截器不放行,postHandle就不会执行。
运行日志信息:
HandlerInterceptor1...preHandle
拦截器1的preHandle不放行,postHandle和afterCompletion不会执行。
拦截器1的preHandle不放行,拦截器2不执行。
根据测试结果,对拦截器应用。
比如:统一日志处理拦截器,需要改拦截器preHandle一定要放行,且将它放在拦截器链中的第一位置。
比如:登录认证拦截器,放在拦截器链中第一个位置。权限校验拦截器,放在登录拦截器之后。(因为登录通过后才校验权限)
(1)用户请求url
(2)拦截器进行拦截校验
如果请求的url是公开地址(无需登录即可访问的url),让放行
如果用户session不存在,跳转到登录页面。
如果用户session存在,放行,继续操作。
<span style="color: #0000ff">package</span><span style="color: #000000"> joanna.yan.ssm.controller; </span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpSession; </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.stereotype.Controller; </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.bind.annotation.RequestMapping; @Controller </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> LoginController { </span><span style="color: #008000">//</span><span style="color: #008000">登录</span> @RequestMapping("/login"<span style="color: #000000">) </span><span style="color: #0000ff">public</span> String login(HttpSession session, String username, String password) <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception{ </span><span style="color: #008000">//</span><span style="color: #008000">调用service进行用户身份认证 </span><span style="color: #008000">//</span><span style="color: #008000">... </span><span style="color: #008000">//</span><span style="color: #008000">在session中保存用户身份信息</span> session.setAttribute("username"<span style="color: #000000">, username); </span><span style="color: #0000ff">return</span> "redirect:items/queryItems.action"<span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000">退出</span> @RequestMapping("/logout"<span style="color: #000000">) </span><span style="color: #0000ff">public</span> String logout(HttpSession session) <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception{ </span><span style="color: #008000">//</span><span style="color: #008000">清除session</span> <span style="color: #000000"> session.invalidate(); </span><span style="color: #0000ff">return</span> "redirect:items/queryItems.action"<span style="color: #000000">; } }</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> LoginInterceptor <span style="color: #0000ff">implements</span><span style="color: #000000"> HandlerInterceptor{ </span><span style="color: #008000">//</span><span style="color: #008000">执行Handler完成执行此方法 </span><span style="color: #008000">//</span><span style="color: #008000">应用场景:统一异常处理,统一日志处理</span> <span style="color: #000000"> @Override </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception { System.out.println(</span>"HandlerInterceptor1......afterCompletion"<span style="color: #000000">); } </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之后,返回modelAndView之前执行 </span><span style="color: #008000">//</span><span style="color: #008000">应用场景从modelAndView出发:将公用的模型数据(比如菜单导航)在这里传到视图,也可以在这里同意指定视图</span> <span style="color: #000000"> @Override </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception { System.out.println(</span>"HandlerInterceptor1......postHandle"<span style="color: #000000">); } </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之前执行 </span><span style="color: #008000">//</span><span style="color: #008000">用于身份认证、身份授权 </span><span style="color: #008000">//</span><span style="color: #008000">比如身份认证,如果认证不通过表示当前用户没有登录,需要此方法拦截不再向下执行。</span> <span style="color: #000000"> @Override </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception { System.out.println(</span>"HandlerInterceptor1......preHandle"<span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000">获取请求的url</span> String url=<span style="color: #000000">request.getRequestURI(); </span><span style="color: #008000">//</span><span style="color: #008000">判断url是否是公开地址(实际使用时要将公开地址配置到文件中) </span><span style="color: #008000">//</span><span style="color: #008000">这里公开地址是登录提交的地址</span> <span style="color: #0000ff">if</span>(url.indexOf("login.action")>=0<span style="color: #000000">){ </span><span style="color: #008000">//</span><span style="color: #008000">如果进行登录提交,放行</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000">判断session</span> HttpSession session=<span style="color: #000000">request.getSession(); String username</span>=(String) session.getAttribute("username"<span style="color: #000000">); </span><span style="color: #0000ff">if</span>(username!=<span style="color: #0000ff">null</span><span style="color: #000000">){ </span><span style="color: #008000">//</span><span style="color: #008000">身份存在,放行</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000">执行到这里,表示用户身份需要认证,跳转登录页面</span> request.getRequestDispatcher("/WEB-INF/jsp/login.jsp"<span style="color: #000000">).forward(request, response); </span><span style="color: #008000">//</span><span style="color: #008000">return false表示拦截,不向下执行 </span><span style="color: #008000">//</span><span style="color: #008000">return true表示放行</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">; } }</span>
classpath下springmvc.xml中配置:
如果此文对您有帮助,微信打赏我一下吧~
以上是Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器的详细内容。更多信息请关注PHP中文网其他相关文章!