SpringMVC 拦截器

1.拦截器

1.1.什么是拦截器?

​ Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、判断用户是否登录等。

​ 拦截器依赖于web框架,在实现上基于Java的反射机制,属于面向切面编程(AOP)的一种运用。

1.2.自定义拦截器

1.2.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 ....");
    }
}

1.2.2.配置拦截器

    
    <mvc:interceptors>
        <mvc:interceptor>
            
            <mvc:mapping path="/**">mvc:mapping>
            <bean class="com.by.interceptor.CustomHandlerInterceptor">bean>
        mvc:interceptor>
    mvc:interceptors>

1.2.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>

1.3.登录拦截器

1.3.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 LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
           HttpServletResponse response, Object object) throws Exception {
        String user = (String) request.getSession().getAttribute("user_session");
        if (user != null) {//已经登录,继续执行
            System.out.println("获得用户信息:"+user);
            return true;
        } else {//未登录,跳转到登录页面
            response.sendRedirect(request.getContextPath() +"/login.jsp");
            return false;
        }
    }
}

1.3.2.配置拦截器

	<mvc:interceptors>
        <mvc:interceptor>
            
            <mvc:mapping path="/**">mvc:mapping>
            <bean class="com.by.interceptor.LoginInterceptor">bean>
        mvc:interceptor>
	mvc:interceptors>

1.3.3.测试

  • 编写controller
@Controller
@RequestMapping("/account")
public class AccountController {

    @RequestMapping("/findAccount13")
    public String findAccount13(Model model) {
        model.addAttribute("msg", "欢迎你 springmvc");
        System.out.println("controller的方法执行了......");
        return "success";
    }
}
  • 在index.jsp里面定义超链接
<%
   session.setAttribute("user_session","User{name:张二狗,age:18}");
%>
<a href="/account/findAccount13">登录拦截器a>

你可能感兴趣的:(Spring,spring,学习,java)