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.
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";
}
}