一、拦截器的概念
SpringMVC 中的 Interceptor 拦截器的主要作用就是拦截用户的 url 请求,并在执行 handler 方法的前中后加入某些特殊请求,类似于 servlet 里面的过滤器。
二、拦截器的开发
定义一个拦截类,实现HandlerInterceptor接口
package com.qjl.ssm.sysmanage.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class InterceptorTest1 implements HandlerInterceptor { // 在执行handler方法之前,运行这个方法里面的代码 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { System.out.println("pre1"); return true; } // 在执行handler方法中,返回modelAndView之前,运行这个方法里面的代码 // 比如微商 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mv) throws Exception { System.out.println("post1"); } // handler方法之后 // 计算某个handler方法的执行时间 // 进行统一的日志记录 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception exp) throws Exception { System.out.println("after1"); } }
在springmvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 只加载Controller层的bean -->
<context:component-scan base-package="com.qjl.ssm" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:interceptors>
<mvc:interceptor>
<!-- mapping和bean必须按照这个顺序 -->
<mvc:mapping path="/**"/>
<bean class="com.qjl.ssm.sysmanage.interceptor.InterceptorTest1"></bean>
</mvc:interceptor>
</mvc:interceptors>
<mvc:resources location="/jsAndCss/" mapping="/jsAndCss/**"></mvc:resources>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
总结:当我们的 preHandler 方法返回false时,剩下的两个方法不执行,而且 handler 也不执行拦截器里面的 preHandler, postHandler 和 afterCompletion 方法是按顺序执行。
当有多个拦截器同时存在时,如果有任意一个拦截器的 preHandler 方法返回 false ,则剩下的不执行。
如果多个拦截器同时运行 ( preHandler 方法返回 true ), 那么 preHanlder 方法是按顺序执行,而其余两个方法是按倒序执行。
拦截器的执行顺序是按照在 springmvc 配置文件中的位置来顺序执行的。
三、登陆验证拦截器实战
需求:只要登陆过的用户才有权限访问系统中的功能
步骤::
- 在登陆成功后,将用户信息放入 session。
- 当除了登陆验证的 url 请求的其他 url 请求时,在拦截器的 preHandler 方法里面判断 session 是否有用户信息, 如果有, 则放行通过, 如果没有 , 跳转到登陆页面,提示用户登陆。
注意:匿名访问问题,不要把登录验证的请求给拦了。