【SpringMVC】拦截器简易使用

前言

一、作用

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值