SpringMVC的处理器拦截器,类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者也可以自己定义一些拦截器来实现特定的功能。
日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。
权限验证:如登录检测,进入处理器检测是否登录,如果没有直接返回到登录页面;
性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反向代理,如apache可以自动记录);
通用行为:读取cookie得到用户信息并将用户对象放入请求,从而方便后续流程使用,还有如提取Locale、Theme信息等,只要是多个Controller中的处理方法都需要的,我们就可以使用拦截器实现。
OpenSessionInView:如Hibernate,在进入处理器打开Session,在完成后关闭Session。
package com.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class DemoInterceptor implements HandlerInterceptor{
// 调用Handler之前
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// true: 按正常流程执行Handler
// false: 不会执行handler
System.out.println("preHandler----处理前");
return HandlerInterceptor.super.preHandle(request, response, handler);
}
// Handler执行完后
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("postHandler----处理后");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
//在dispatcherServlet处理后执行,进行一些资源清理。
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("afterCompletion----资源清理");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
<!--关于拦截器的配置-->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截所有请求-->
<mvc:mapping path="/**" />
<!--bean配置的就是拦截器-->
<bean class="com.interceptor.DemoInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/inte")
public class DemoController4 {
@GetMapping("/test1")
public String test1() {
System.out.println("test1.......执行了");
return "demo";
}
}
打开浏览器访问 http://localhost:8080/SpringMVC2/inte/test1
拦截用户的请求,判断用户是否登陆成功,登陆成功则放行,否则就跳转到登陆页面。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<base href="<%=request.getContextPath() %>/"/>
<title>Insert title here</title>
</head>
<body>
<form action="user/login" method="post">
UserName:<input type="text" name="userName"/><br/>
Password:<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/login")
public String loginPage() {
return "user/login";
}
@PostMapping("/login")
public String login(String userName,String password,HttpSession session) {
if(userName != null && password != null) {
session.setAttribute("Lotus", userName);
}
return "user/welcome";
}
@GetMapping("/test")
public String test() {
return "test";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>welcome</title>
</head>
<body>
Hello, ${Lotus}
</body>
</html>
package com.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 判断是否有登录(输入有账号密码,userName就会存储到session)
Object value = request.getSession().getAttribute("Lotus");
if (value != null) {
return true;
}else {
request.getRequestDispatcher("/WEB-INF/views/user/login.jsp").forward(request, response);
return false;
}
}
}
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!--拦截user下面所有的请求-->
<mvc:mapping path="/user/**"/>
<!--放行user路径下的login-->
<mvc:exclude-mapping path="/user/login"/>
<!--bean配置的就是拦截器-->
<bean class="com.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>