Spring MVC拦截器

2018-07-01  本文已影响27人  沧海一粟谦

SpringMvc的拦截器和过滤器的区别

1.拦截器是使用JDK动态代理实现的,拦截的是对应调用方法的拦截
2.过滤器是使用Filter实现的,拦截的是request对象(用户发起的http请求)

SpringMvc拦截器的配置

SpringMvc可以使用拦截器对请求的路由方法进行拦截处理,用户也可以自定义拦截器来实现特定的功能,自定义拦截器必须实现HandlerInterceptor接口,该接口中有三个方法:

应用场景

使用

web.xml配置springmvc的核心servlet有三种方式

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
  
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 配置自定义扫描包 -->
    <context:component-scan base-package="com"></context:component-scan>

     <!--拦截器的注册-->
    <mvc:interceptors>

        <!--可以注册多个拦截器-->
        <!--<mvc:interceptor>-->
            <!--<mvc:mapping path="/user/**"/>-->
            <!--<bean class="com.interceptor.LoginInterceptor"></bean>-->
        <!--</mvc:interceptor>-->
        
        <mvc:interceptor>
            <mvc:mapping path="/user/**"/>
            <!--exclude-mapping在所有拦截中进行排除,一般在通配符会有意义-->
            <mvc:exclude-mapping path="/user/index"></mvc:exclude-mapping>
            <bean class="com.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

LoginController.java

@Controller
public class LoginController {

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/dologin")
    public String dologin(@RequestParam("name") String name,
                          @RequestParam("password") String password,
                          HttpSession session){
        if (name.equals("JoinSnow") && password.equals("123456")){
            User user = new User();
            user.setName(name);
            user.setPassword(password);
            session.setAttribute("user",user);
            return "redirect:user/admin";
        }else {
            return "redirect:login";
        }
    }
}

UserController.java

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/admin")
    public String admin(HttpSession session){
        System.out.println("进入 admin()");
        return "admin";
    }

    @RequestMapping("/index")
    public String index(HttpSession session){
        System.out.println("进入 index()");
        return "index";
    }
}

Spring MVC 提供了一个HandlerInterceptor 接口,我们可以实现自定义的拦截器。
LoginInterceptor.java

public class LoginInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        User user = (User)httpServletRequest.getSession().getAttribute("user");
        if (user == null){
            System.out.println("进入:preHandle");
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login");
            return false;//如果返回false,终止所有请求
        }
        return true; //返回true才会进入到控制器里面的方法
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("进入:postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("进入:afterCompletion");
    }
}

执行顺序:preHandle()、 admin()、postHandle、afterCompletion
login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>登陆页面</h1>
    <div>
        <form action="dologin" method="post">
            <p><input type="text" name="name"></p>
            <p><input type="text" name="password"></p>
            <p><input type="submit" name="" value="提交"></p>
        </form>
    </div>
</body>
</html>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>首页测试</h1>
</body>
</html>

admin.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>后台管理</title>
</head>
<body>
<h2>你当前登录的用户是:${user.name}</h2>
</body>
</html>

测试

访问:http://localhost:8080/login 登陆之后跳转到 admin.jsp
直接访问:http://localhost:8080/user/admin 被拦截
直接访问:http://localhost:8080/user/index 不会被拦截

多个拦截器的访问顺序

上一篇 下一篇

猜你喜欢

热点阅读