SpringMVC-拦截器
拦截器类似于过滤器,可以设置拦截规则(拦截所有的请求)
public class Interceptor1 implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("执行前1");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("执行后1");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("页面渲染后1");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
<!-- 拦截器配置 -->Springmvc.xml
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截规则 http://localhost:8080/05-10/user/queryUsers.do-->
<mvc:mapping path="/**"/>
<bean class="cn.yunhe.interceptor.Interceptor1"></bean>
</mvc:interceptor>
<mvc:interceptor>
<!-- 拦截规则 http://localhost:8080/05-10/user/queryUsers.do-->
<mvc:mapping path="/**"/>
<bean class="cn.yunhe.interceptor.Interceptor2"></bean>
</mvc:interceptor>
</mvc:interceptors>
1、当有多个拦截器时,那么执行前的方法是按照拦截器的配置顺序执行的,其他的方法是按照配置顺序逆序执行;
2、当有多个拦截器时,如果有一个拦截器拦截了请求并返回false,那么后续的拦截器就不再执行,之前的拦截器中的postHandler方法也不再执行;
案例-登录访问权限控制
流程:用户直接访问列表页,判断用户是否登录,未登录就跳转到登录页面,登录成功后跳转到列表页
用户是否已登录,根据是否在session域中存储了用户名
步骤:
1、登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/login.do" method="post">
<input type="text" name="uname">
<input type="password" name="upwd">
<input type="submit" value="login">
</form>
</body>
</html>
2、请求:跳转到登录页面的请求,登录页的登录请求
/**
* 接收跳转到登录页的请求
* @return
*/
@RequestMapping(value="/login.do",method = RequestMethod.GET)
public String login() {
return "login";
}
/**
* 表单提交时发送的请求
* 登录成功后再次发送列表页的请求地址
* @param uname
* @param session
* @return
*/
@RequestMapping(value="/login.do",method = RequestMethod.POST)
public String login2(String uname,HttpSession session) {
session.setAttribute("uname", uname);
return "redirect:/user/queryUsers.do";
}
3、拦截器:拦截所有的请求(判断是否是跳转页登录请求)
public class Interceptor1 implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
//配置的是拦截所有的请求
//http://localhost:8080/05-11/user/login.do
//区别当前的请求是否是login.do,需要拿到当前的请求地址
//URL:http://localhost:8080/05-11/user/login.do
//URI:05-11/user/login.do
String uri = request.getRequestURI();
if(!uri.contains("login.do")) {
//判断当前用户是否是已登录的状态
String uname = (String) request.getSession().getAttribute("uname");
if(null == uname) {
response.sendRedirect(request.getContextPath() + "/user/login.do");
return false;
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("执行后1");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("页面渲染后1");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}