Spring MVC第六讲(SpringMVC-拦截器、案例-登录访问权限控制)

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值