springmvc拦截器

1、SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor 来实现的。在SpringMVC 中定义一个Interceptor 非常简单,主要有两种方式,第一种方式是要定义的Interceptor类要实现了spring 的HandlerInterceptor 接口,或者是这个类继承实现了HandlerInterceptor 接口的类,比如Spring 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInterceptorAdapter ;第二种方式是实现Spring的WebRequestInterceptor接口,或者是继承实现了WebRequestInterceptor的类。


2、继承抽象类HandlerInterceptorAdapter

  1. package com.kp.spring.interceptor;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. import org.springframework.web.bind.annotation.ResponseBody;  
  7. import org.springframework.web.method.HandlerMethod;  
  8. import org.springframework.web.servlet.ModelAndView;  
  9. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;  
  10.   
  11. import com.kp.spring.util.SystemUtils;  
  12.   
  13.   
  14. /**  
  15.  * @author: py 
  16.  * @version:2016年10月17日 上午11:28:43  
  17.  * com.kp.spring.interceptor.TestInterceptor.java 
  18.  * @Desc  SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求 
  19.  *  并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那样子判断当前时间是 
  20.  *  否是购票时间。 
  21.  */  
  22.   
  23. /** 
  24.  * 输入: 
  25.  * http://localhost:8080/SpringMvcKP/test/test.do 
  26.  * 结果: 
  27.      时间:2016-10-22 16:32:15;类:com.kp.spring.interceptor.TestInterceptor;path:/test/test.do 
  28.      时间:2016-10-22 16:32:15;类:com.kp.spring.interceptor.TestInterceptor;contextPath:/SpringMvcKP 
  29.      时间:2016-10-22 16:32:15;类:com.kp.spring.interceptor.TestInterceptor;name:com.kp.spring.controller 
  30.  * 
  31.  */  
  32. public class TestInterceptor extends HandlerInterceptorAdapter {  
  33.   
  34.       
  35.     /**  
  36.      * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用, 
  37.      * SpringMVC中的Interceptor拦截器是链式的,可以同时存在 多个Interceptor, 
  38.      * 然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在  
  39.      * Controller方法调用之前调用。 
  40.      * SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返  
  41.      * 回值为false, 
  42.      * 当preHandle的返回值为false的时候整个请求就结束了。  
  43.      */  
  44.     @Override  
  45.     public boolean preHandle(HttpServletRequest request,  
  46.             HttpServletResponse response, Object handler) throws Exception {  
  47.         SystemUtils.println(TestInterceptor.classnull"****1preHandle");  
  48.         String path = request.getServletPath();  
  49.         String contextPath = request.getContextPath() ;  
  50.         SystemUtils.println(TestInterceptor.class"path", path);  
  51.         SystemUtils.println(TestInterceptor.class"contextPath", contextPath);  
  52.           
  53.         if (handler instanceof HandlerMethod) {  
  54.             HandlerMethod handlerMethod = (HandlerMethod) handler;  
  55.             if(handlerMethod.getMethodAnnotation(ResponseBody.class)!=null){  
  56.                 //ajax 请求不拦截  
  57.                 return true;  
  58.             }  
  59.             // 访问的不是WX下的,直接跳过  
  60.             String name = handlerMethod.getBeanType().getPackage().getName();  
  61.             SystemUtils.println(TestInterceptor.class"name", name);  
  62.             if (!name.contains("com.oo.project")) {  
  63.                   
  64.                 return true;  
  65.             }    
  66.         }  
  67.         return true;  
  68.     }  
  69.       
  70.       
  71.       
  72.       
  73.     /**  
  74.      * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。 
  75.      *  
  76.      * postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之后,也就是在Controller 
  77.      * 的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中 
  78.      * 你可以对ModelAndView进行操作。 
  79.      * 这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用, 
  80.      * 这跟Struts2里面的拦截器的执行过程有点像, 只是Struts2里面的intercept方法中要手动的调用 
  81.      * ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下 
  82.      * 一个Interceptor或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前, 
  83.      * 要在Interceptor之后调用的内容都写在调用invoke方法之后。  
  84.      */  
  85.     @Override  
  86.     public void postHandle(HttpServletRequest request,  
  87.             HttpServletResponse response, Object handler,  
  88.             ModelAndView modelAndView) throws Exception {  
  89.         SystemUtils.println(TestInterceptor.classnull"****2postHandle");  
  90.           
  91.         super.postHandle(request, response, handler, modelAndView);  
  92.           
  93.     }  
  94.       
  95.       
  96.       
  97.     /**  
  98.      * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。 
  99.      * 该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,  
  100.      * 这个方法的主要作用是用于清理资源的, 
  101.      * 当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。  
  102.      */   
  103.     @Override  
  104.     public void afterCompletion(HttpServletRequest request,  
  105.             HttpServletResponse response, Object handler, Exception ex)  
  106.             throws Exception {  
  107.         SystemUtils.println(TestInterceptor.classnull"****3afterCompletion");  
  108.           
  109.         super.afterCompletion(request, response, handler, ex);  
  110.           
  111.     }  
  112.       
  113.       
  114.       
  115. }  
package com.kp.spring.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.kp.spring.util.SystemUtils;


/** 
 * @author: py
 * @version:2016年10月17日 上午11:28:43 
 * com.kp.spring.interceptor.TestInterceptor.java
 * @Desc  SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求
 * 	并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那样子判断当前时间是
 * 	否是购票时间。
 */

/**
 * 输入:
 * http://localhost:8080/SpringMvcKP/test/test.do
 * 结果:
	 时间:2016-10-22 16:32:15;类:com.kp.spring.interceptor.TestInterceptor;path:/test/test.do
	 时间:2016-10-22 16:32:15;类:com.kp.spring.interceptor.TestInterceptor;contextPath:/SpringMvcKP
	 时间:2016-10-22 16:32:15;类:com.kp.spring.interceptor.TestInterceptor;name:com.kp.spring.controller
 *
 */
public class TestInterceptor extends HandlerInterceptorAdapter {

	
	/** 
     * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,
     * SpringMVC中的Interceptor拦截器是链式的,可以同时存在 多个Interceptor,
     * 然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在 
     * Controller方法调用之前调用。
     * SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返 
     * 回值为false,
     * 当preHandle的返回值为false的时候整个请求就结束了。 
     */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		SystemUtils.println(TestInterceptor.class, null, "****1preHandle");
		String path = request.getServletPath();
		String contextPath = request.getContextPath() ;
		SystemUtils.println(TestInterceptor.class, "path", path);
		SystemUtils.println(TestInterceptor.class, "contextPath", contextPath);
		
		if (handler instanceof HandlerMethod) {
			HandlerMethod handlerMethod = (HandlerMethod) handler;
			if(handlerMethod.getMethodAnnotation(ResponseBody.class)!=null){
				//ajax 请求不拦截
				return true;
			}
			// 访问的不是WX下的,直接跳过
			String name = handlerMethod.getBeanType().getPackage().getName();
			SystemUtils.println(TestInterceptor.class, "name", name);
			if (!name.contains("com.oo.project")) {
				
				return true;
			}  
		}
		return true;
	}
	
	
	
	
	/** 
     * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。
     * 
     * postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之后,也就是在Controller
     * 的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中
     * 你可以对ModelAndView进行操作。
     * 这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,
     * 这跟Struts2里面的拦截器的执行过程有点像, 只是Struts2里面的intercept方法中要手动的调用
     * ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下
     * 一个Interceptor或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,
     * 要在Interceptor之后调用的内容都写在调用invoke方法之后。 
     */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		SystemUtils.println(TestInterceptor.class, null, "****2postHandle");
		
		super.postHandle(request, response, handler, modelAndView);
		
	}
	
	
	
	/** 
     * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。
     * 该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行, 
     * 这个方法的主要作用是用于清理资源的,
     * 当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。 
     */ 
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		SystemUtils.println(TestInterceptor.class, null, "****3afterCompletion");
		
		super.afterCompletion(request, response, handler, ex);
		
	}
	
	
	
}



3、两个需拦截的controller 

①MyController

  1. package com.kp.spring.controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5.   
  6.   
  7. @Controller  
  8. public class MyController {  
  9.   
  10.       
  11.     @RequestMapping("my.do")  
  12.     public void my(){  
  13.         System.out.println("test my ");  
  14.     }  
  15. }  
package com.kp.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class MyController {

	
	@RequestMapping("my.do")
	public void my(){
		System.out.println("test my ");
	}
}

②YouController

  1. package com.kp.spring.controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5.   
  6.   
  7. @Controller  
  8. @RequestMapping("you")  
  9. public class YouController {  
  10.   
  11.       
  12.     @RequestMapping("you.do")  
  13.     public void you(){  
  14.         System.out.println("test you ");  
  15.     }  
  16. }  
package com.kp.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("you")
public class YouController {

	
	@RequestMapping("you.do")
	public void you(){
		System.out.println("test you ");
	}
}


4、配置文件如果是

  1. <mvc:interceptors>  
  2.         <mvc:interceptor>  
  3.             <mvc:mapping path="/*"/>  
  4.             <bean class="com.kp.spring.interceptor.TestInterceptor"/>  
  5.         </mvc:interceptor>  
  6.     </mvc:interceptors>   
<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/*"/>
			<bean class="com.kp.spring.interceptor.TestInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors> 

浏览器输入:http://localhost:8080/SpringMvcKP/my.do

控制台输出:

  1. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle  
  2. test my  
  3. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle  
  4. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion  
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle
 test my
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion


浏览器输入:http://localhost:8080/SpringMvcKP/you/you.do

控制台输出:test you


结果:

成功拦截http://localhost:8080/SpringMvcKP/my.do

拦截 http://localhost:8080/SpringMvcKP/you/you.do失败



5、配置文件如果是

  1. <mvc:interceptors>  
  2.         <mvc:interceptor>  
  3.             <mvc:mapping path="/**"/>  
  4.             <bean class="com.kp.spring.interceptor.TestInterceptor"/>  
  5.         </mvc:interceptor>  
  6.     </mvc:interceptors>   
<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.kp.spring.interceptor.TestInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors> 

浏览器输入:http://localhost:8080/SpringMvcKP/my.do

控制台输出:

  1. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle  
  2. test my  
  3. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle  
  4. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion  
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle
 test my
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion


浏览器输入:http://localhost:8080/SpringMvcKP/you/you.do

控制台输出:

  1. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle  
  2. test you  
  3. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle  
  4. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion  
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle
 test you
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion

结果:

成功拦截http://localhost:8080/SpringMvcKP/you/you.do

成功拦截http://localhost:8080/SpringMvcKP/my.do


6、配置文件如果是,

  1. <mvc:interceptors>  
  2.         <mvc:interceptor>  
  3.             <mvc:mapping path="/you/*"/>  
  4.             <bean class="com.kp.spring.interceptor.TestInterceptor"/>  
  5.         </mvc:interceptor>  
  6.     </mvc:interceptors>   
<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/you/*"/>
			<bean class="com.kp.spring.interceptor.TestInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors> 

浏览器输入:http://localhost:8080/SpringMvcKP/my.do

控制台输出:test my

浏览器输入:http://localhost:8080/SpringMvcKP/you/you.do

控制台输出:

  1. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle  
  2. test you  
  3. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle  
  4. 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion  
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle
 test you
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle
 时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion

结果:

拦截http://localhost:8080/SpringMvcKP/my.do失败

成功拦截http://localhost:8080/SpringMvcKP/you/you.do


7、配置文件如果是,

  1. <mvc:interceptors>  
  2.         <mvc:interceptor>  
  3.             <mvc:mapping path="/"/>  
  4.             <bean class="com.kp.spring.interceptor.TestInterceptor"/>  
  5.         </mvc:interceptor>  
  6.     </mvc:interceptors>   
<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/"/>
			<bean class="com.kp.spring.interceptor.TestInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors> 

浏览器输入:http://localhost:8080/SpringMvcKP/my.do

控制台输出:test my

浏览器输入:http://localhost:8080/SpringMvcKP/you/you.do

控制台输出:test you

结果:

拦截http://localhost:8080/SpringMvcKP/my.do失败

拦截http://localhost:8080/SpringMvcKP/you/you.do失败

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值