public class HandlerInterceptor1 implements HandlerInterceptor { // 執(zhí)行Handler完成之后,執(zhí)行此方法 // 應(yīng)用場景:統(tǒng)一的異常處理,統(tǒng)一的日志處理 @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO 自動生成的方法存根 } // 進(jìn)入Handler方法之后,在返回ModelAndView之前執(zhí)行 // 應(yīng)用場景:從ModelAndView出發(fā):將公用的模型數(shù)據(jù)等,在這里傳到視圖,也可以在這里統(tǒng)一指定視圖 // 比如菜單層級 @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO 自動生成的方法存根 } // 進(jìn)入Handler方法之前執(zhí)行 // 應(yīng)用場景:身份認(rèn)證、身份授權(quán) // 比如身份認(rèn)證,如果認(rèn)證不通過標(biāo)識當(dāng)前用戶沒有登錄,需要此方法攔截不再向下執(zhí)行 @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { // return flase 表示攔截,不再向下執(zhí)行 // return true 表示放行 return false; }}攔截器配置1.針對HandlerMapping攔截器(一般不推薦使用,比較麻煩)springmvc攔截器針對HandlerMapping進(jìn)行攔截設(shè)置。如果在某個HandlerMapping中配置攔截,經(jīng)過該HandlerMapping映射成功的Handler最終使用該攔截器2.類似全局的攔截器(推薦使用)springmvc配置類似全局的攔截器,springmvc框架將配置的類似全局的攔截器注入到每個HandlerMapping中。第一種配置方式:<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="handlerInterceptor1"/> <ref bean="handlerInterceptor2"/> </list> </property></bean> <bean id="handlerInterceptor1" class="springmvc.intercapter.HandlerInterceptor1"/> <bean id="handlerInterceptor2" class="springmvc.intercapter.HandlerInterceptor2"/>第二種配置方式:<!--攔截器 --><mvc:interceptors> <!--多個攔截器,順序執(zhí)行 --> <!--/**表示攔截所有的url包括子url路徑,/*攔截最根的url --> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="sys.um.interceptor.HandlerInterceptor1"></bean> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="sys.um.interceptor.HandlerInterceptor2"></bean> </mvc:interceptor></mvc:interceptors>多個攔截器執(zhí)行順序preHandle按攔截器定義順序調(diào)用postHandler按攔截器定義逆序調(diào)用afterCompletion按攔截器定義逆序調(diào)用 postHandler在攔截器鏈內(nèi)所有攔截器返成功調(diào)用afterCompletion只有preHandle返回true才調(diào)用應(yīng)用場景:比如:統(tǒng)一日志處理攔截器,需要攔截器preHandler一定要放行,且將它放在攔截器鏈接中的第一個位置比如:登錄認(rèn)證攔截器,放在攔截器鏈接中第一個位置,權(quán)限校驗攔截器,放在登錄認(rèn)證攔截器之后。(因為登錄通過后才校驗權(quán)限,如果有日志處理攔截器,登錄認(rèn)證攔截器放在日志攔截器之后)攔截器應(yīng)用需求:實(shí)現(xiàn)登錄認(rèn)證1、用戶請求url2、攔截器進(jìn)行攔截校驗如果請求的url是公開地址(無需登錄即可訪問url),放行如果用戶session不存在,跳轉(zhuǎn)到登錄界面。如果用戶session存在,放行,繼續(xù)操作。攔截器內(nèi)容:public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception { // return flase 表示攔截,不再向下執(zhí)行 // return true 表示放行 String url = request.getRequestURI(); // 過濾掉登錄頁面驗證 if (url.indexOf("doLogin") >= 0) { return true; } if (request.getSession().getAttribute("userName") == null) { request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); return false; } return true; }登錄Controller:@RequestMapping("/doLogin")@RequestMapping("/doLogin") public String doLogin(HttpSession session, String userName, String userPass) throws Exception { session.setAttribute("userName", userName); return "redirect:/user/queryUsers.action"; } @RequestMapping("/doLoginOut") public String doLoginOut(HttpSession session) throws Exception { session.invalidate(); return "redirect:/doLogin.action"; } public String doLogin(HttpSession session, String userName, String userPass) throws Exception { session.setAttribute("userName", userName); return "redirect:/user/queryUsers.action"; } @RequestMapping("/doLoginOut") public String doLoginOut(HttpSession session) throws Exception { session.invalidate(); return "redirect:/doLogin.action"; }login界面:<form action="${pageContext.request.contextPath }/doLogin.action" method="post"> 名稱:<input name="userName" /><br /> 密碼:<input name="userPass" /><br /> <input type="submit" value="提交" /> </form>主界面:<c:if test="${userName!=null }">${userName},<a href="${pageContext.request.contextPath }/doLoginOut.action">退出</a></c:if>
新聞熱點(diǎn)
疑難解答