###定義攔截器java
實現HandlerInterceptor方法。web
###攔截器配置spring
springmvc 攔截器針對HandlerMapping進行攔截設置。若是HandlerMapping映射成功才進行攔截。session
<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="club.lemos.ssm.interceptor.HandlerInterceptor1"/> <bean id="handlerInterceptor2" class="club.lemos.ssm.interceptor.HandlerInterceptor1"/>
springmvc 配置類型全局的攔截器,springmvc框架將配置的相似全局的攔截器注入到每一個HandlerMapping中。mvc
public class HandlerInterceptor1 implements HandlerInterceptor { //進入Handler以前 //用於身份驗證、身份受權 @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("preHandle...interceptor1"); return true; } //在Handler以後,在DispatcherServlet進行渲染以前 //應用場景從ModelAndView出發,能夠將公用模型數據傳導視圖(好比菜單導航)。也能夠在這裏統一指定視圖。 @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle...interceptor1"); } //在渲染視圖以後 //統一的異常處理。統一的日誌處理。 @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion...interceptor1"); } }
###攔截器規律app
例如:若要進行統一日誌處理,須要將第一個Prehandler放行。框架
###攔截器的應用(實現登陸認證)ide
1、需求:post
2、登陸controllerurl
LoginController .java
@Controller public class LoginController { //用戶登陸 @RequestMapping("/login") public String login(Model model, HttpSession session, String username, String password, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //調用service 進行驗證 //這裏沒有驗證,所以點擊登陸會直接進入商品界面。 //... session.setAttribute("username", username); if (username == null || username.isEmpty()) { //當用戶名爲null或者爲空時,讓他登陸。 return "login"; } else if (!username.equals("張三")) { model.addAttribute("msg", "用戶名或者密碼錯誤!"); return "login"; } return "redirect:/items/queryItems"; } //用戶註銷 @RequestMapping("/logout") public String logout(HttpSession session) { session.invalidate(); return "redirect:/items/queryItems"; } }
3、登陸攔截實現
loginInterceptor.java
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { //判斷url是不是公開地址 String url = httpServletRequest.getRequestURI(); if (url.contains("login")) { return true; } //判斷session是否存在 HttpSession session = httpServletRequest.getSession(); String username = (String) session.getAttribute("username"); if (username != null && !username.isEmpty()) { System.out.println("名字"+username+"。"); return true; } //攔截的處理:打到url爲login的Handler,讓它去處理 httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login"); return false; //反回false,表示攔截。 }
4、注意要配置攔截器映射
springmvc-servlet.xml
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="club.lemos.ssm.interceptor.LoginInterceptor"/> </mvc:interceptor> </mvc:interceptors>
注意:不能重定向到WEB-INF下的網頁。若要重定向到此網頁,必須在controller中,且該controller有requestMapping 的url。