1、什么是拦截器?
Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、判断用户是否登录等。
拦截器依赖于web框架,在实现上基于Java的反射机制,属于面向切面编程(AOP)的一种运用。
1.1创建拦截器
package com.by.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CustomHandlerInterceptor implements HandlerInterceptor {
/**
* 在控制器方法调用前执行
* 返回值为是否中断
* true:表示继续执行(下一个拦截器或处理器)
* false:则会中断后续的所有操作,所以我们需要使用response来继续响应后续请求
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object object) throws Exception {
System.out.println("HandlerInterceptor preHandle ....");
return true;
}
/**
* 在控制器方法调用后,解析视图前调用,我们可以对视图和模型做进一步渲染或修改
* 可在modelAndView中加入数据,比如当前时间
*/
@Override
public void postHandle(HttpServletRequest request,HttpServletResponse response,
Object object, ModelAndView modelAndView) throws Exception {
System.out.println("HandlerInterceptor postHandle ....");
}
/**
* 整个请求完成,即视图渲染结束后调用,这个时候可以做些资源清理工作,或日志记录
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,Object object, Exception e) throws Exception {
System.out.println("HandlerInterceptor afterCompletion ....");
}
}
/**
* handler执行后,返回jsp后
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
1.2配置拦截器
<!--配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 匹配的是url路径 -->
<mvc:mapping path="/**"></mvc:mapping>
<bean class="com.by.interceptor.CustomHandlerInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
1.3测试
-
编写controller
@Controller
@RequestMapping("/account")
public class AccountController {
@RequestMapping("/findAccount12")
public String findAccount12(Model model) {
model.addAttribute("msg", "欢迎你 springmvc");
System.out.println("controller的方法执行了......");
return "success";
}
}
-
在index.jsp里面定义超链接
<a href="/account/findAccount12">拦截器</a>
2、案例,配置登录拦截器
2.1创建拦截器
package com.zhy.interceptor;
import com.zhy.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("-----------------------------------------------------------");
//判断是否登录
User loginUser = (User) request.getSession().getAttribute("loginUser");
if (loginUser!=null){
return true;//如果登录,则进行放行
}
response.sendRedirect("/login.jsp");
return false;//不放行
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
2.2配置拦截器
<!-- 配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!-- 匹配的是url路径 -->
<mvc:mapping path="/**"></mvc:mapping>
<!-- 放行以下请求-->
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/register"/>
<mvc:exclude-mapping path="/user/toRegister"/>
<bean class="com.zhy.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
2.3测试
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
// 注册功能
@RequestMapping("/register")
public String register(){
return "register";
}
@RequestMapping("/toRegister")
public String toRegister(User user){
userService.toRegister(user);
return "redirect:/login.jsp";
}
//登录
@RequestMapping("/login")
public String login(User user, HttpSession session){
User loginUser =userService.login(user);
if(loginUser!=null){
session.setAttribute("loginUser",loginUser);
return "redirect:/account/selectAccount";
}
return "redirect:/login.jsp";
}
}