前言
一、作用
Spring MVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理。
将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(Interceptor Chain)。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。拦截器也是AOP思想的具体实现
1.拦截器和过滤器区别
二、使用步骤
1. 说明
2. 创建拦截器类
代码如下(示例):
public class MyHandlerInterceptor1 implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) {
System.out.println("preHandle running...");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse
response, Object handler, ModelAndView modelAndView) {
System.out.println("postHandle running...");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse
response, Object handler, Exception ex) {
System.out.println("afterCompletion running...");
}
}
3. 配置拦截器
代码如下(示例):
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.txt.interceptor.MyHandlerInterceptor1"/>
</mvc:interceptor>
</mvc:interceptors>
4. 拦截效果
@RequestMapping("/quick23")
@ResponseBody
public ModelAndView quickMethod23() throws IOException, ParseException {
System.out.println("目标方法执行....");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name","txt");
modelAndView.setViewName("index");
return modelAndView;
}
结果
preHandle running--.
目标方法执行....
postHandle running...
afterCompletion running. ..
5. 多拦截器效果
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.txt.interceptor.MyHandlerInterceptor1"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.txt.interceptor.MyHandlerInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>
public class MyHandlerInterceptor2 implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) {
System.out.println("preHandle running222...");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse
response, Object handler, ModelAndView modelAndView) {
System.out.println("postHandle running222...");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse
response, Object handler, Exception ex) {
System.out.println("afterCompletion running222...");
}
}
结果
preHandle running ...
preHandle running222...
目标方法执行....
postHandle running222...
postHandle running...
afterCompletion running222...
aftercompletion running. . .
三、 简易案例
Controller层调用Service层的接口登录
@RequestMapping("/login")
public String login(String username,String password,HttpSession session){
User user = userService.login(username,password);
if(user!=null){
//登录成功 将user存储到session
session.setAttribute("user",user);
return "redirect:/index.jsp";
}
return "redirect:/login.jsp";
}
拦截器 进行用户登录权限控制
public class PrivilegeInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
//逻辑:判断用户是否登录 本质:判断session中有没有user
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if(user==null){
//没有登录
response.sendRedirect(request.getContextPath()+"/login.jsp");
return false;
}
//放行 访问目标资源
return true;
}
}